Some notable places when configuring the 404 error page in the Nginx server _nginx

Source: Internet
Author: User
Tags curl response code nginx server

One day after the VPS was changed, Google Administrator tool under the console to see a lot of "soft 404" error, looked for some data found that it is under the Nginx to configure the 404 page of the method does not lead to the error of the production, this record nginx under the correct 404 page configuration method.

404 is a code that says "the page cannot be found" (pages not Found), and Google's "soft 404" statement is:

Copy Code code as follows:

Instead of returning a 404 response code for a non-existent URL, websites so serve "soft 404s" return a response cod E.

This means that for URLs that do not exist, the server does not return 404 (Page not Found) code, but instead returns the (OK) code, which is not normal.

And then in the other search results, I saw this conversation again.

Copy Code code as follows:

Soft 404s can occur as a result of configuration errors to an Error Document 404 is specified as a absolute path rather than a relative path.


After reading it dawned, because my 404 custom pages are with pictures and CSS, and the picture and CSS are in the relative path (eg./xxx/xxx) written on the page, so in order to allow the whole station can see 404 pages of the picture, I put 404 pages in Nginx defined as absolute path (eg.//www.slyar.com/xxx/xxx), because the page is treated as an external page, it returns 200 code, resulting in a "soft 404" error.

Know the wrong, it is good to do. The 404 page path is defined as a relative path, as with pictures and CSS, as long as the absolute path is used on the page.

Nginx the correct 404 page Definition method:

1, vim edit nginx configuration file, with the vhosts on the separate change, no use of direct change nginx.conf

Vim/usr/local/nginx/conf/nginx.conf

Or

Vim/usr/local/nginx/conf/vhosts/slyar.com.nginx.conf

2, the relative path to specify 404 pages

server {
#error_page 404//www.slyar.com/404.html
error_page 404/404.html;
}

3.: Wq save exit, Reload Nginx

/usr/local/nginx/sbin/nginx-s Reload

4, re-examine the nonexistent page, see whether to return 404

Curl-i//www.slyar.com/slyar

http/1.1 404 Not Found server:nginx/1.0.15 date:mon
, Aug (08:13:56) c4/>content-type:text/html
content-length:2110
connection:keep-alive

5. Hide Nginx error page and the version number on header
Nginx the Nginx version number on the error page, such as 403 and 404, is very unsafe, and hackers may know how to hack into your server through your Nginx version number, because certain versions of the server program may have vulnerabilities.

The Nginx version number usually appears in 2 places:

1. HTTP headers, such as server:nginx/1.x.x, will expose the name of the software used by the Web server and the version number

2, on the 403 and 404 error pages, default will display Nginx 1.x.x version information

Hiding Nginx version number is very simple, the official has given a very good solution, using Server_tokens (Whether to send the Nginx version of the error pages and Server header)

Open the profile nginx.conf, and then add the following parameters to the HTTP section

HTTP {
... Other configuration
server_tokens off;


Then nginx-s reload overload nginx configuration, so you can hide the Nginx version number. You can use the Curl Self test.

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.