Nginx + PHP Query Strings

Nginx (Engine-X) is the sweetest web server I’ve used. It’s running my VPS now, CPU usage has barely budged, even with a decent number of visitors. I ran into a problem, which I was searching for a solution before I cracked at it myself. The solution ended up being incredibly simple, but still may help someone else who’s searchin’ for it.

With nginx, PHP is passed of to FastCGI PHP process to parse and execute. It relies on a location rule to find out what are PHP files. But it would fall apart using URLs like:

index.php/some/query/string/parameters

Apache doesn’t have any trouble, with it, but nginx, if you looked at the error log, it’ll be about the directory not existing. (Hmm, parsing that in PHP sounds like a good idea for another post… noted).

So if we check out the current rewrite rules:

[code]
location ~ \.php$ {
include /etc/nginx/conf/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/public/$fastcgi_script_name;
}
[/code]

The fix is pretty simple:

[code]
location ~ \.php<strong>(.*)</strong>$ {
include /etc/nginx/conf/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/public/$fastcgi_script_name;
}
[/code]

Adding in the (.*) after the .php allows the PHP files to be picked up, and those additional query string parameters will get passed along into FastCGI.

Simple fix!

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • HackerNews
  • Netvibes
  • Reddit
  • StumbleUpon
  • Twitter
  • Yahoo! Buzz

2 Responses - Add Yours+

  1. Nick says:

    Howdy,

    What do you use to spawn your FastCGI PHP processes? I’ve read about getting that part from LightTPD, what do you recommend?

    - Nick

  2. Nabeel says:

    Hey,

    That’s what I do, I just use the same way to spawn the processes. The guide I used was this:
    http://jit.nuance9.com/2008/01/serving-php5-with-nginx-on-ubuntu-710.html

    Hope that helps

Leave a Reply