Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

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 at 14:19

Your Answer

 
discard

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.