Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx - Customizing 404 page

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?

like image 486
thomas55 Avatar asked Jun 21 '09 15:06

thomas55


People also ask

How do I create a custom 404 page in nginx?

Create a configuration file called custom-error-page. conf under /etc/nginx/snippets/ as shown. This configuration causes an internal redirect to the URI/error-page. html every time NGINX encounters any of the specified HTTP errors 404, 403, 500, and 503.

What is 404 not found nginx?

Essentially, the “404 error” indicates that your or your visitor's web browser was connected successfully to the website server or the host. However, it was unable to locate the requested resource, such as filename or any specific URL.


1 Answers

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:

location /my_blog {     error_page    404 /blog_article_not_found.html; } 

A site wide 404 page:

server {     listen 80;     error_page  404  /website_page_not_found.html;     ... 

You can append standard error codes together to have a single page for several types of errors:

location /my_blog {     error_page 500 502 503 504 /server_error.html } 

To redirect to a totally different server, assuming you had an upstream server named server2 defined in your http section:

upstream server2 {     server 10.0.0.1:80; } server {     location /my_blog {         error_page    404 @try_server2;     }     location @try_server2 {         proxy_pass http://server2;     } 

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.

like image 136
Great Turtle Avatar answered Sep 22 '22 18:09

Great Turtle