2

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_pass 127.0.0.1:9000;
                    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!

Splodge
  • 21
  • 1

2 Answers2

1

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

Vlad
  • 187
  • 1
  • 10
1

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.

kolbyjack
  • 8,039
  • 2
  • 36
  • 29