Protip: URL Parameters with Nginx & PHP

Firefox

If you’re using nginx as your webserver and need to pass URL parameters to your PHP front controller then some adjustments to your configuration file are necessary:

location /www {
    try_files $uri $uri/ /www/index.php?$args;
}

The longer explanation:

I’m recenlty worked on a small PHP application built with Silex and served up with nginx. The application makes use of Twitter’s API with OAuth 1.0 (which in my opinion is very difficult to use compared to Facebook’s OAuth2 implementation.)

OAuth 1.0 requires GET/POSTing with urls like:

/www/twitter/auth?oauth_token=XXX&oauth_verifier=XXX

Everything was working fine on my laptop under Apache, but not when I moved it to my dev server under nginx. Some logging revealed that the URL parameters were not getting picked up by Silex’s Request class.

I took a look at my nginx.conf and found the culprit, the redirect to the front controller was setup like so:

location /www {
    try_files $uri $uri/ /www/index.php;
}

This doesn’t pass any URL parameters to index.php, so I simply added ?$args to the redirect:

location /www {
    try_files $uri $uri/ /www/index.php?$args;
}

and booya! everything is working again.

Reference: nginx + php-fpm - where are my $_GET params?

Comments