Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this set up in an nginx site config, which is working perfectly to redirect all .html requests through to /index.php. However it doesn't rewrite test.php to /index.php and instead throws a 404 error.

Can anyone shed any light please? Here is my site config:

server {
    listen 80 default_server;

    location / {
            root /srv/www/htdocs;
            index index.php;

            try_files $uri $uri/ /index.php;

            location ~* \.php$ {
                    fastcgi_param HTTPS on; # <-- add this line
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include /etc/nginx/fastcgi_params;
                    fastcgi_buffer_size 128k;
                    fastcgi_buffers 256 4k;
                    fastcgi_busy_buffers_size 256k;
                    fastcgi_temp_file_write_size 256k;
                    fastcgi_intercept_errors on;

Any help appreciated - I'm tearing my hair out!

share|improve this question

location ~* \.php$ will match any *.php and cause try_files to return it regardless of its existence (hence the 404). Consider using rewrite instead!

share|improve this answer

When a try_files directive is put directly into a server, it is only evaluated if no location matches, so it doesn't take effect if a request already ends with .php. The way to fix this is to add a try_files $uri /index.php =404; inside the php location.

share|improve this answer
I'm having this same problem. Is there a way to do this at the site level rather than the server level? – Matt Mombrea Oct 3 '14 at 14:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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