Вопрос

I'm having a hard time getting Restler to work properly with nginx. In the first place I couldn't even get Math.php/add to work (returned 404).

But I managed this way (I've seen a lot of people struggle with this):

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;
...

Attention: cgi.fix_pathinfo=1

Everything works fine now except that I cant receive any GET parameter using ?parameter=

For example, the Math example doesn't work.

If I call /api/math/add?n1=6&n2=4 it returns 2

But the multiply example works: GET /math/multiply/4/3 returns {"result":12}

<?php
class Math
{
    /**
     * @param int $n1
     * @param int $n2
     *
     * @return int
     */
    function add($n1 = 1, $n2 = 1)
    {
        return $n1 + $n2;
    }
}

routes.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,
);

$_SERVER variable on index.php

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
)
Это было полезно?

Решение

Solved with this:

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

    if (!-d $request_filename) {
        rewrite ^(.*)$ /api/index.php last;
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top