12

I am a beginner with nginx and php, so please excuse my basic question.

For a RESTful based API (nginx + php) I would need some help with nginx configuration.

Here is the relevant snippet of the nginx configuration (as suggested here) for redirecting all /api/v1/* requests to my apiv1.php script:

    server {
        server_name myServer;
        root /usr/share/nginx/html;
        location /api/v1/ {
          try_files $uri $uri/ /apiv1.php?$args;
        }

        location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

Now the issue is that when I type http://myServer//api/v1/resource/GetInfo in my browser, the apiv1.php script doesn't seem to receive the "resource/GetInfo". Actually, _GET and _REQUEST are empty, but _SERVER looks OK!

In my /etc/php5/fpm/php.ini, the following relevant config is enabled:

request_order = "GP"
variables_order = "GPCS"
register_argc_argv = Off
auto_globals_jit = On.

Do you maybe know why the php _GET and _REQUEST are empty? Is this related to my php configuration only?

Best regards, M.

2
  • $args only contains GET parameters (i.e. query strings). So in your example you would be appending the query strings (if any) to the fallback uri in your try_files directive. Commented Jan 22, 2015 at 22:50
  • Thanks, looks like I missed that one. Commented Jan 23, 2015 at 15:40

2 Answers 2

16

Replace this:

location /api/v1/ {
    try_files $uri $uri/ /apiv1.php?$args;
}

With the following inside your server block:

rewrite ^/api/v1/([^/]+)/([^/]+)/?$ /apiv1.php?class=$1&method=$2? last;

Create a php file called apiv1.php and place in the root directory of your web server with the following lines of code:

<?php
$class  = filter_input(INPUT_GET, 'class',  FILTER_SANITIZE_STRING);
$method = filter_input(INPUT_GET, 'method', FILTER_SANITIZE_STRING);

echo $class;
echo '<br />';
echo $method;

Test by visiting the following link in your browser:

http://myServer/api/v1/members/getInfo
2
  • Great, now my _GET and _REQUEST are no longer empty :) Well, I'm one step forward because you helped me, so thanks a lot! :) Commented Jan 23, 2015 at 15:43
  • This is a good answer -- I initially got the common File not Found error PHP. Then I ran: tail /var/log/nginx/error.log and "Unable to open primary script: /var/www/[myDomain]/html/apiv1.php -- so renamed index.php to apiv1.php and it worked.
    – Sean Dev
    Commented Feb 21, 2018 at 17:32
5

If someone else hit this page, there is solution I got for myself after a bit research:

location ~ ^/api/v0/(.*)/?$ {
    try_files $uri $uri/ /v0.php?req=$1&$args;
}

Here I'm not limited with class/method structure and location seems more readable than rewrite.

1
  • This should be the accepted answer. Good job. Concise, complete and useful.
    – marcinx
    Commented Mar 29, 2018 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.