NGINX configuration 404 error page redirection

Source: Internet
Author: User

NGINX configuration 404 error page redirection

What is the 404 page?

If a website error occurs, or the user attempts to access a page that does not exist, the Server Returns Error Code 404. The corresponding page is the 404 page. The default content of the 404 page is related to the specific server. If the NGINX server is used in the background, the 404 page content is:

404 Not Found

Nginx

Why do we need to customize the 404 page?

When I encounter the above 404 error page, I think 99% (uninvestigated, estimated data) users will close the page, and users will be quietly lost. If there is a beautiful page at this time to guide the user to where he wants to go, the user will inevitably be retained. Therefore, each website should customize its 404 page.

How to customize the 404 page in NGINX

There have been a lot of articles about the experience of customizing the 404 page under IIS and APACHE, and NGINX is still relatively small. Coincidentally, some of my servers are NGINX, in order to solve their own problems, we have made in-depth research on this. The research results show that it is feasible to configure a custom 404 page in NGINX, and it is very simple, just a few steps:

1.create the secret 404.html page
2. Change nginx. conf and add it to the http definition area:

fastcgi_intercept_errors on;

3. Add nginx. conf in the server region:

error_page 404 = /404.html

4. Verify nginx. conf:

/usr/local/nginx/sbin/nginx –t

If the information is correct, the following information should be displayed:

the configuration file /usr/local/nginx/conf/nginx.conf syntax is okconfiguration file /usr/local/nginx/conf/nginx.conf test is successful

5. Restart nginx

kill -HUP `cat /usr/local/nginx/nginx.pid `

Configuration file instance:
......

Http {include mime. types; default_type application/octet-stream; charset gb2312; Limit 128; Limit 32 k; limit 4 32 k; limit 8 m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64 k; fastcgi_buffers 4 64 k; fastcgi_busy_buf Fers_size 128 Kb; fastcgi_temp_file_write_size 128 k; fastcgi_intercept_errors on; gzip on; gzip_min_length 1 k; gzip_buffers 4 16 k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; # limit_zone crawler $ binary_remote_addr 10 m; # webarch.org configuration information server {listen 80; server_name www.webarch.org webarch.org; index index.html index.htm index. p Hp; root/www/webarch.org; location ~. * \. (Php | php5 )? $ {# Fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1: 9000; fastcgi_index index. php; include fcgi. conf;} error_page 404 =/404.html; #502 and other errors can be configured in the same way. Error_page 500 502 503 504 =/50x.html; ocation =/50x.html {root html ;} log_format webarch.org '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ status $ response" $ http_referer "'' "$ http_user_agent" $ http_x_forwarded_for '; access_log/usr/local/nginx/logs/webarch.org. log webarch.org ;}......

Note:
1. You must add: fastcgi_intercept_errors on. If this token is not set, creating 404.html and configuring error_page does not work.

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 when this command is set to on, nginx supports 404 error redirection. Note that if fastcgi_intercept_errors on is set, nginx will redirect the 404 error. In nginx, the precondition for 404 error redirection to take effect is that fastcgi_intercept_errors on is set, and error_page is set correctly (including syntax and corresponding 404 page. do not set the homepage as a 404 error page for the purpose of saving time or increasing the homepage weight, or use other methods to jump to the homepage.

3. The custom 404 page must be larger than 512 bytes; otherwise, the default 404 page of IE may appear. For example, the parameter is set to 404.html and the size is only 11 bytes (the content is 404 error ). Use the following two addresses that do not exist for access:

The http://www.webarch.org/no will invoke the self-defined 404.html

Http://www.webarch.org/notfound:

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.