Nginx defines the 404 page and returns the 404 status code

Source: Internet
Author: User
Tags error handling object object

WebServer is nginx, which directly tells me that they have configured the nginx 404 error page. Although a 404 page can be successfully returned for a resource that does not exist in the request, the return status code is 200.
404. html


This is 404 page.
Request a page that does not exist:

Ajax Code:

The code is as follows: Copy 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 result:

Go to the Object to see details:

The requested page is not found and the 404 page information is returned, but the status code is still 200. Therefore, jQuery directly calls the success callback instead of the error callback function.
There should be a problem with configuration writing, so I opened nginx. conf and found that their configuration was written like this:
Error_page 404 =/404.html;
So I read the official documentation and changed the above expression:
Error_page 404/404 .html;
Then restart


D: ginx-1.5.11> nginx.exe-s reload
Try again:

Let's take a look at the ajax request:

Obviously, compared to the previous one, the status code 404 is returned, which is displayed in console. log below:
[Object Object], error, Not Found
Then click to enter the Object to see:

The status value is 404. Both the 404 page and the 404 status code are returned, so that ajax requests can determine the page request status based on the status code for error handling.

 

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.