Nginx configuration 404

Source: Internet
Author: User
Tags nginx server
NGINX Configuration 404 error page TurnWhat is 404 page

If a problem happens to the site, or if the user tries to access a page that does not exist, the server returns an error message with code 404, and the corresponding page is 404 pages. 404 The default content of the page is related to the specific server. If the background is Nginx server, then the content of the 404 page is: 404 Not Found

Why do you want to customize 404 pages

When I encountered the 404 error page above, I thought that 99% (without investigation, estimated data) would turn off the page and the user would be quietly lost. If you can have a beautiful page at this time to guide users to where he wants to go, it is inevitable to retain users. Therefore, each site should customize its own 404 pages.

How to customize 404 pages under Nginx

The experience of customizing 404 pages Under IIS and Apache is already very much, Nginx is still relatively few, in order to solve their own problems specifically for this in-depth study. The results show that it is feasible and simple to configure a custom 404 page under Nginx, with just the following steps:

1. Create your own 404.html page

2. Change nginx.conf in the HTTP definition area add: fastcgi_intercept_errors on;

3. Change nginx.conf (or a separate Web site profile, such as a site profile under Nginx-> sites-enabled)

Add in the server area: Error_page 404/404.html or error_page 404 = http://www.xxx.com/404.html

4. Restart Nginx After the change, test nginx.conf correctness:/opt/nginx/sbin/nginx–t

Errors such as #502 can be configured in the same way.

Error_page 502 503 504/50x.html;


Precautions:

1. You must add: fastcgi_intercept_errors on; If this option is not set, even if the 404.html is created and the Error_page is configured, there is no effect. Fastcgi_intercept_errors syntax: fastcgi_intercept_errors on|off default: fastcgi_intercept_errors off Add Location: http, server, Location by default, Nginx does not support custom 404 error pages, and only this instruction is set to On,nginx to support the redirection of 404 errors. It is important to note that the Fastcgi_intercept_errors On,nginx is not set to redirect the 404 error. In Nginx, the 404 error Redirection takes effect on the premise that fastcgi_intercept_errors on is set and the Error_page option (including syntax and corresponding 404 pages) is set correctly.

2. Do not for the sake of convenience or improve the weight of the home page to specify the first page as 404 error pages, and do not use other methods to jump to the home page.

3. A custom 404 page must be greater than 512 bytes, otherwise the IE default 404 page may appear. For example, imagine a custom 404.html with a size of 11 bytes (content: 404 error). Http://www.jyboke.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.