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.

What is the best way of handling non-existent PHP pages in Nginx? I have the following location block...

location ~ \.php$ {
    fastcgi_pass   localhost:9000;
    include        fastcgi_params;
}

I'd like to be able to display a 404 error if a PHP page doesn't exist, I'm guessing I need to get the try_files (http://wiki.nginx.org/HttpCoreModule#try_files) directive involved by handling all errors with the error_page directive (declared elsewhere), but am unsure of the best way of doing this.

Any help would be appreciated.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

just use error_page(you can change page_not_found.html to php script):

error_page 404 /page_not_found.html;
location ~ \.php$ {
    fastcgi_pass   localhost:9000;
    include        fastcgi_params;
    fastcgi_intercept_errors on;
}

try file need to redirect to a different server, if page not found.

share|improve this answer
    
That doesn't appear to work, is there something I'm missing in the 'fastcgi_params' include perhaps? –  m.p.c Mar 1 '11 at 12:10
    
Try add fastcgi_intercept_errors on –  ooshro Mar 1 '11 at 12:15
    
Perfect, thank you kindly! –  m.p.c Mar 1 '11 at 12:37
    
Uh. Try files does not need to redirect to a different server... –  Martin Fjordvald Mar 1 '11 at 13:58
add comment

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.