Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure 404 pages in Nginx and how to return 404 pages by AJAX request

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the "Nginx 404 page configuration and AJAX request to return 404 page" related knowledge, editor through the actual case to show you the process of operation, the method of operation is simple and fast, practical, hope that this "Nginx 404 page configuration and AJAX request to return 404 page" article can help you solve the problem.

Basic configuration of 404 pages

404 errors are easy to occur when visiting www websites. The most common error prompt: 404 not found. The setting of the error page has a great impact on the seo of the website, while the improper setting, such as directly switching to the home page, will be plucked by the search engine. The purpose of the 404 page should be to tell the user that the page you requested does not exist and to guide the user to other pages of the site instead of closing the window and leaving. Search engines use http status codes to identify the status of web pages. When the search engine gets an error link, the site should return a 404 status code, telling the search engine to abandon the index of the link. If a 200 or 302 status code is returned, the search engine will index the link, resulting in a large number of different links pointing to the same page content. As a result, the trust of search engines in the site has been greatly reduced.

The following is the lnmp setup nginx 404 error page tutorial:

1 、

Vi / usr/local/nginx/conf/nginx.conf

Edit the nginx configuration file and add the following code to the http section:

Fastcgi_intercept_errors on

2. Edit the website configuration file, such as this site:

Vi / usr/local/nginx/conf/vhost/onelone.com.conf

To add the following code to the server section

Error_page 404 = / 404.html

Note: some netizens testing the uplink code need to remove the equal sign to return to the correct 404 status, so please test whether you want to remove the equal sign.

3. Test whether the configuration file is correct:

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

And return the following code through:

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

4. Restart lnmp takes effect: / root/lnmp restart.

5. Precautions for making 404 error pages:

(1) do not redirect the 404 error to the home page of the site, otherwise the home page may be downgraded or disappear in the search engine.

(2) do not use absolute url, if you use absolute url, the status code returned is 302q200, which will result in a large number of duplicate web pages.

(3) 404 page setup is complete, be sure to check whether it is correct. The http header message must return a status of 404. This can be checked by the server header information checking tool.

(4) do not automatically jump to the 404 page, let the user decide where to go.

Custom 404 pages must be larger than 512 bytes, otherwise ie default 404 pages may appear.

The 404 page requested by ajax returns

A few days ago, a friend had a problem with the program, but no matter how to check it, I didn't see it, so let me take a look at it. In fact, ajax requests a lot of templates and then writes them to the page. The key is that all the requested pages are returned with 200 normal status codes. On the surface, there is no problem. In fact, some requests return 200 status codes, but the status codes returned are 200. Webserver is nginx, which directly tells me that they have configured the 404 error page of nginx. Although the request for a resource that does not exist can successfully return the 404 page, the status code is 200.

404.htmlthis is 404 page.

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 result:

Go to object to see the details:

The requested page cannot be found and the 404 page information is returned, but the status code is still 200, so jquery does not use the error function callback but directly uses the success callback.

There should be something wrong with the configuration, so open nginx.conf and find that their configuration reads as follows:

Error_page 404 = / 404.html

So I looked up the documents on the official website and rewrote the above expression as:

Error_page 404 / 404.html

And then restart.

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

Let's try again:

Let's take a look at what ajax requests:

Obviously, compared to the previous one that returns the red 404 status code, the following console.log shows

[object object], error,not found

Then click to enter object to see:

The status value is 404. It can not only return 404 pages, but also return 404 status code so that the ajax request can judge the status of the page request according to the status code for error handling.

Here are some additions from other netizens:

1. Why the error page of nginx is displayed elegantly?

When we visit the website, due to special reasons, errors such as 403404503 often occur, which greatly affects the user's access experience, so it is necessary for us to do an elegant display of the error page in order to improve the user's browsing experience.

2. How to define an elegantly displayed page under nginx?

Let's take the 404 error as an example. The specific steps are as follows:

1. Create your own 404.html page and put it under the site directory

two。 Change the nginx.conf configuration file to add fastcgi_intercept_errors on to the http module

3. Change the nginx.conf configuration file and add: error_page 404 / 404.html to the server module; or error_page 404 = http://www.hulala.com/404.html;

4. Check the syntax / nginx/sbin/nginx-t after the change, and restart nginx

Now, the elegant display of the 404 error page has been configured with ok.

Take three examples: 502, 403 and other errors can be configured in the same way.

Error_page 500 502 503 504 / 50x.html

Error_page 403 / 403.html

Note:

The two prerequisites for error redirection to take effect in nginx are that fastcgi_intercept_errors on is set and the option error_page is set correctly.

This is the end of the content about "how to configure 404 pages in Nginx and AJAX request to return 404 pages". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report