Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of configuring 404 error page redirection on NGINX server

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

What is a 404 page?

If there happens to be a problem with the site, or if the user tries to access a page that does not exist, the server will return an error message with the code 404, which is the 404 page. The default content of the 404 page is related to the specific server. If you are using a NGINX server in the background, the content of the 404 page is: 404 Not Found

Why customize the 404 page?

When I encounter the above 404 error page during the visit, I think 99% of the users (unsurveyed, estimated data) will shut down the page, and the users will be lost quietly. If there is a beautiful page that can guide the user to where he wants to go, he will be able to retain the user. Therefore, each website should customize its own 404 pages.

How to customize 404 pages under NGINX

There are a lot of articles about the experience of customizing 404 pages under IIS and APACHE, but there are few about NGINX at present. In order to solve our own problems, we have done in-depth research on this. The research results show that it is feasible to configure custom 404 pages under NGINX, and it is very simple, just the following steps are needed:

1. Create your own 404.html page

two。 Change nginx.conf to add: fastcgi_intercept_errors on in the http definition area

3. Change nginx.conf (or individual site profile, such as site profile under nginx-> sites-enabled)

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

4. Restart nginx, after change to test the correctness of nginx.conf: / opt/nginx/sbin/nginx-t

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

Error_page 500502 503 504 = / 50x.html

Note:

1. Must be added: fastcgi_intercept_errors on; if this option is not set, even if 404.html is created and error_page is configured, it will have 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, only this instruction is set to on,nginx to support the redirection of 404 errors. It is important to note that setting fastcgi_intercept_errors on,nginx does not mean that 404 errors will be redirected. The premise that the error redirection takes effect in nginx is that fastcgi_intercept_errors on is set and error_page is set correctly (including syntax and corresponding 404 pages).

two。 Do not specify the home page as a 404 error page for the purpose of saving trouble or increasing the weight of the home page, and do not jump to the home page in other ways.

3. Custom 404 pages must be larger than 512 bytes, otherwise IE default 404 pages may appear. For example, suppose you customize the 404.html with a size of only 11 bytes (error 404).

Customizing the nginx 404 error page is a detail to improve the user experience, if it is a regular site, we must do it well. You can set up a separate error page for each site, or you can set a global 404 page.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report