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/0.8.6

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 that can 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 the problem of self-owned companies, we have made in-depth research on this issue. 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 a secret 404.html page

2.Change nginx. conf to add in the http definition area:

Fastcgi_intercept_errors on;

3.Change nginx. conf to add in the server region:

Error_page 404 =/404.html

4.Verify nginx. conf:

/Opt/nginx/sbin/nginx-t

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

The configuration file/opt/nginx/conf/nginx. conf syntax is OK
Configuration file/opt/nginx/conf/nginx. conf test is successful

Kill-HUP 'cat/opt/nginx. Pi'

Configuration file instance:

......

  1. Http
  2. {
  3. Include mime. types;
  4. Default_type application/octet-stream;
  5. Charset gb2312;
  6. Server_names_hash_bucket_size 128;
  7. Client_header_buffer_size 32 k;
  8. Large_client_header_buffers 4 32 k;
  9. Client_max_body_size 8 m;
  10. Sendfile on;
  11. Tcp_nopush on;
  12. Keepalive_timeout 60;
  13. Tcp_nodelay on;
  14. Fastcgi_connect_timeout 300;
  15. Fastcgi_send_timeout 300;
  16. Fastcgi_read_timeout 300;
  17. Fastcgi_buffer_size 64 k;
  18. Fastcgi_buffers 4 64 k;
  19. Fastcgi_busy_buffers_size 128 k;
  20. Fastcgi_temp_file_write_size 128 k;
  21. Fastcgi_intercept_errors on;
  22. Gzip on;
  23. Gzip_min_length 1 k;
  24. Gzip_buffers 4 16 k;
  25. Gzip_http_version 1.0;
  26. Gzip_comp_level 2;
  27. Gzip_types text/plain application/x-javascript text/css application/xml;
  28. Gzip_vary on;
  29. # Limit_zone crawler $ binary_remote_addr 10 m;


#65 configuration information

  1. Server
  2. {
  3. Listen 80;
  4. Server_name www.65.la 65.la *. 65.la;
  5. Index index.html index.htm index. php;
  6. Root/opt/www/65;
  7. Location ~ . * \. (Php | php5 )? $
  8. {
  9. # Fastcgi_pass unix:/tmp/php-cgi.sock;
  10. Fastcgi_pass 127.0.0.1: 9000;
  11. Fastcgi_index index. php;
  12. Fcinclude GI. conf;
  13. }
  14. Error_page 404 =/404.html;


#502 and other errors can be configured in the same way.

  1. Error_page 500 502 503 504 =/50x.html;
  2. Ocation =/50x.html {
  3. Root html;
  4. }
  5. Log_format 65 '$ remote_addr-$ remote_user [$ time_local] "$ request "'
  6. '$ Status $ body_bytes_sent "$ http_referer "'
  7. '"$ Http_user_agent" $ http_x_forwarded_for ';
  8. Access_log/opt/nginx/logs/65.log 65;
  9. }


......

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 page, only 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 the error_page option is set correctly (including syntax and corresponding 404 page)

2. do not set the homepage as the 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:


Q: Where can I store the created 404 page?
I haven't tried it yet


@ 5169.info
Place it anywhere. you can change the configuration of this line.
Error_page 404 =/404.html;



@ 5169.info
Place it anywhere. you can change the configuration of this line.
Error_page 404 =/404.html;

That is to say, each sub-site copies a 404 File. can this be the case?
Error_page 404 = ../404.html;
Only put a set of 404 under htdocs


No trackbacks or pingbacks are available in this document.
Note: Comments are allowed to use'@ User :'To notify other reviewers of their comments.
For example, ABC is one of the reviewers of this article. If '@ ABC does not include single quotation marks,' @ ABC will automatically send your comments to ABC.
The user must match the reviewer's name to distinguish between the size and write.

Start.jpg (412.91 KB, Downloads: 35)

Download attachment

404

Upload


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.