Nginx+PHP (on fastCGI) works great for me. When I enter a path to a PHP file which doesn't exist, instead of getting the default 404 error page (which comes for any invalid .html file), I simply get a "No input file specified.".
How can I customize this 404 error page?
You use the error_page property in the nginx config.
For example, if you intend to set the 404 error page to
/404.html
, useSetting the 500 error page to
/500.html
is just as easy as:The "error_page" parameter is not enough.
The easiest solution is
By the way, if you want Nginx to process 404 status returned by PHP scripts, you need to add
E.g.
Be careful with the syntax! Great Turtle used them interchangeably, but:
error_page 404 = /404.html;
Will return the 404.html page with a status code of 200 (because = has relayed that to this page)
error_page 404 /404.html;
Will return the 404.html page with a (the original) 404 error code.
https://serverfault.com/questions/295789/nginx-return-correct-headers-with-custom-error-documents
You can setup a custom error page for every location block in your nginx.conf, or a global error page for the site as a whole.
To redirect to a simple 404 not found page for a specific location:
A site wide 404 page:
You can append standard error codes together to have a single page for several types of errors:
To redirect to a totally different server, assuming you had an upstream server named server2 defined in your http section:
The manual can give you more details, or you can search google for the terms nginx.conf and error_page for real life examples on the web.
These answers are no longer recommended since
try_files
works faster thanif
in this context. Simply addtry_files
in your php location block to test if the file exists, otherwise return a 404.