The correct way to define 404 pages in Nginx and return 404 status Codes _nginx

Source: Internet
Author: User
Tags error handling object object

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

Copy Code code as follows:

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 (Tex tstatus+ ":" +response);
},
error:function (XMLHttpRequest, Textstatus, Errorthrown) {
CONSOLE.L    OG ([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 directly to success the callback.

There should be a problem with the configuration writing, so open nginx.conf and find that they are configured to write:

Copy Code code as follows:

Error_page 404 =/404.html;

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

Copy Code code as follows:

Error_page 404/404.html;

and then reboot.

Copy Code code as follows:

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

Copy Code code as follows:

[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.

(End of this 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.