Nginx 404 page configuration and Ajax request to return 404 pages of the method _nginx

Source: Internet
Author: User
Tags error handling object object

404 Page Base Configuration
the 404 error is an error that is easy to access on the WWW site. The most common error tip: 404 Not FOUND. 404 error page settings for Web site SEO has a great impact, but not set up, such as direct jump home, etc., will be the search engine down the right to pull hair. The purpose of the 404 page should be to tell the user that the page you are requesting does not exist, and to guide the user through other pages of the site instead of turning off the window. Search engines use HTTP status codes to identify the status of Web pages. When the search engine gets a bad link, the site should return a 404 status code, telling the search engine to discard the index on the link. If you return a 200 or 302 status code, the search engine will index the link, which leads to a large number of different links pointing to the same page content. As a result, search engines have significantly reduced their trust in the site.
The following is the LNMP Setup nginx 404 error page Tutorial:
1,

 
 

Edit the Nginx configuration file and add the following code in the HTTP section:

Fastcgi_intercept_errors on;

2, edit the site configuration files, such as this site:

 
 

, add the following code in the server section:

Error_page 404 =/404.html; 

Note: There are users to test the uplink code need to remove the equal sign to return the correct 404 status, so please students themselves test whether to remove the equal sign.

3, test the configuration file is correct:

/usr/local/nginx/sbin/nginx-t

, return the following code through:

The configuration file/usr/local/nginx/conf/nginx.conf syntax is OK
configuration file/usr/local/nginx/conf/ Nginx.conf Test is successful

4, restart LNMP effective:/root/lnmp restart.

5, 404 error page Production considerations:
(1) Do not turn the 404 error to the homepage of the website, otherwise it may cause the homepage to be lowered or disappeared in the search engine.
(2) Do not use an absolute URL, if the use of absolute URL to return the status code is 302+200, this will produce a large number of duplicate pages.
(3) 404 Page Setup Complete, be sure to check whether it is correct. HTTP header information must be returned in 404 states. This can be checked by the Server header information Check tool.
(4) 404 pages do not automatically jump, let users decide where to go.
The custom 404 page must be greater than 512 bytes, otherwise the IE default 404 page may appear.

Ajax-requested 404 page return
a few days ago, a friend of the program out of the problem but how to check all did not see problems, so let me help it look. In fact, it is an AJAX request for a number of templates, and then write the template to the page. The key is that all of the requested pages are 200 normal status code return, seemingly no problem, in fact, some requests, although return 200 status code, but give back the status code is 200. Webserver is nginx, just tell me that they configured the Nginx 404 error page, although the request nonexistent resources can successfully return 404 pages, but the return status code is indeed 200.

404.html This is
404 page.

To request a page that does not exist:

Ajax Code:

$.ajax ({
 URL: "does_not_exist.html",
 success:function (Response, textstatus) {
 Console.log (textstatus+ ":" +response);
 },
 error:function (XMLHttpRequest, Textstatus, Errorthrown) {
 Console.log ([ XMLHttpRequest, Textstatus, Errorthrown].join (","));
 }
;

Execution results:

Enter object to see details:

Could not find the requested page and return 404 page information, but the status code is still 200, so jquery did not take the error function callback but went straight success callback.
There should be a problem with the configuration writing, so open nginx.conf and find that they are configured to write:

Error_page 404 =/404.html;

So I looked at the official website document and rewrote the expression above to read:

Error_page 404/404.html;

and then reboot.

D:\nginx-1.5.11>nginx.exe-s Reload

Try again:

Let's look at the AJAX request:

Visible compared to the previous return marked red 404 status Code, the following Console.log out is

[Object Object],error,not Found

Then click Enter object to see:

The status value is 404. Both the return of 404 pages, and the return of 404 status code so that AJAX requests can be based on the status code to determine the status of the page request error handling.

The following are additional users:

A. Nginx the reason for the graceful display of the error page?
When we visit the site, for special reasons, often appear such as 403,404,503 errors, which greatly affect the user's access experience, so it is necessary to do the elegant display of the wrong page to enhance the user's browsing experience.

Two. How do you define a page that is elegantly displayed under Nginx?
Let's take the 404 error, for example, with the following steps:
1. Create your own 404.html page, placed under the site directory;
2. Change the nginx.conf configuration file and add Fastcgi_intercept_errors on to the HTTP module;
3. Change nginx.conf configuration file, add in server module: Error_page 404/404.html; or error_page 404 =http://www.hulala.com/404.html;
4. Check grammar/nginx/sbin/nginx-t after change, and restart Nginx;
Now, the 404 wrong page Elegance Display has been configured OK.

Extrapolate: 502, 403 and other errors can be configured in the same way.
Error_page 502 503 504/50x.html;
Error_page 403/403.html;
Attention:
The two prerequisites for error redirection in Nginx are: Set fastcgi_intercept_errors on, and set the Error_page option correctly.

Related Article

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.