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!


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

| 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.

| 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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