Nginx - Route all requests to single script

I have a PHP script that handles script routing and does all sorts of fancy things. It was originally designed for Apache, but I'm trying to migrate it to nginx for a few of my boxes. Right now, I'm trying to smooth things out on a test server.

So the way the script works is that it intercepts all HTTP traffic for the directory (in Apache) using a .htaccess file. Here's what that looks like:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.+$ index.php [L]
</IfModule>

Pretty straightforward. All requests are run through index.php, plain and simple.

I'm looking to mimic that behavior on nginx, but I haven't yet found a way. Anybody have any suggestions?

Here's a copy of my nginx.conf file at the moment. Note that it was designed for me to just try to get it working; mostly a copy/paste job.

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
        include         /etc/nginx/mime.types;
        default_type    text/plain;
        include         /etc/nginx/conf.d/*.conf;
        server {
                listen          80;
                server_name     swingset.serverboy.net;

                access_log      /var/log/nginx/net.serverboy.swingset.access_log;
                error_log       /var/log/nginx/net.serverboy.swingset.error_log warn;

                root            /var/www/swingset;

                index           index.php index.html;
                fastcgi_index   index.php;

                location ~ \.php {
                        include /etc/nginx/fastcgi_params;
                        keepalive_timeout 0;
                        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                        fastcgi_pass    127.0.0.1:9000;
                }
        }
}

Add this,

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

What it does is it first check the existence of $uri and $uri/ as real files/folders and if they don't exist will just go through /index.php (that is my setup for Zend framework where routing is done through index.php) - of course if you need to pass some parameters, just append to the /index.php a ?q= at the end and it will pass the parameters.

Make sure that the try_file directive is available from version 0.7.27 and onward.


I figured it out on my own! Yeah!

All I needed for the location block was:

location /  {
    include /etc/nginx/fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME  $document_root/index.php;
    fastcgi_pass    127.0.0.1:9000;
}

Everything else remained largely the same.