nginx で Restler を正しく動作させるのに苦労しています。そもそも、Math.php/add を動作させることさえできませんでした (404 が返されました)。
しかし、私はこの方法で管理しました(多くの人がこれに苦労しているのを見てきました):
root /var/www/test/;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass unix:/var/run/php5-fpm/socket.socket;
fastcgi_index index.php;
include fastcgi_params;
}
location /api { //map /api to public directory
try_files $uri /api/public/index.php;
}
fastcgi_param
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
...
注意: cgi.fix_pathinfo=1
?parameter= を使用して GET パラメータを受け取ることができないことを除いて、すべてが正常に機能するようになりました。
たとえば、数学の例は機能しません。
/api/math/add?n1=6&n2=4 を呼び出すと、2 が返されます
しかし、乗算の例は機能します: GET /math/multiply/4/3 は {"result":12} を返します。
<?php
class Math
{
/**
* @param int $n1
* @param int $n2
*
* @return int
*/
function add($n1 = 1, $n2 = 1)
{
return $n1 + $n2;
}
}
ルート.php
//==== GET v1/math/add ====
$o['GET']['v1/math/add'] = array (
'className' => 'Math',
'path' => 'v1/math',
'methodName' => 'add',
'arguments' =>
array (
'n1' => 0,
'n2' => 1,
),
'defaults' =>
array (
0 => 1,
1 => 1,
),
'metadata' =>
array (
'description' => '',
'longDescription' => '',
'param' =>
array (
0 =>
array (
'type' => 'int',
'name' => 'n1',
'default' => 1,
'required' => false,
'from' => 'query',
),
1 =>
array (
'type' => 'int',
'name' => 'n2',
'default' => 1,
'required' => false,
'from' => 'query',
),
),
'return' =>
array (
'type' => 'int',
'description' => '',
),
'resourcePath' => 'v1/math/',
),
'accessLevel' => 0,
);
index.php の $_SERVER 変数
Array
(
[TEMP] => /tmp
[TMPDIR] => /tmp
[TMP] => /tmp
[PATH] => /usr/local/bin:/usr/bin:/bin
[HOSTNAME] =>
[USER] => ftp
[HOME] => /var/www/
[FCGI_ROLE] => RESPONDER
[QUERY_STRING] =>
[REQUEST_METHOD] => GET
[CONTENT_TYPE] => application/x-www-form-urlencoded
[CONTENT_LENGTH] =>
[SCRIPT_FILENAME] => /var/www/test/api/public/index.php
[SCRIPT_NAME] => /api/public/index.php
[PATH_INFO] =>
[REQUEST_URI] => /api/math/add?n1=6&n2=4
[DOCUMENT_URI] => /api/public/index.php
[DOCUMENT_ROOT] => /var/www/test
[SERVER_PROTOCOL] => HTTP/1.1
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_SOFTWARE] => nginx/1.4.2
[HTTPS] =>
[REDIRECT_STATUS] => 200
[HTTP_CONNECTION] => keep-alive
[HTTP_CACHE_CONTROL] => no-cache
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
[HTTP_CONTENT_TYPE] => application/x-www-form-urlencoded
[HTTP_ACCEPT] => */*
[HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
[HTTP_ACCEPT_LANGUAGE] => pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
[PHP_SELF] => /api/public/index.php
[REQUEST_TIME_FLOAT] => 1380738349.0903
[REQUEST_TIME] => 1380738349
)