0

我很难让 Restler 与 nginx 一起正常工作。首先,我什至无法让 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
)
4

1 回答 1

0

解决了这个问题:

location /api {
    if (!-f $request_filename) {
        rewrite ^(.*)$ /api/index.php last;
    }

    if (!-d $request_filename) {
        rewrite ^(.*)$ /api/index.php last;
    }
}
于 2013-10-11T11:18:51.167 回答