Nginx - Route all requests to single script

Solution 1:

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.

Solution 2:

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.


Solution 3:

To preserve GET arguments use the following:

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

$is_args becomes '?' if $args in not empty

Or even more simple:

location / {
    try_files /index.php$is_args$args;
}

Tags:

Nginx

Php