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

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

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.