There are some things to note when configuring a 404 error page in the Nginx server

  • 2020-05-10 23:26:49
  • OfStack

One day after the change of VPS, I saw a large number of "soft 404" errors in the console of Google administrator tools. After looking up some information, I found that it was the wrong way to configure the 404 page under Nginx that caused the error. Record 1 correct 404 page configuration method under Nginx.

404 is a corresponding code that means "page could not be found "(Page Not Found).


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

So instead of returning 404(Page Not Found) code for those URL that don't exist, the server returns 200(OK) code, which is not normal.

Later, I saw this paragraph in other search results


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

After watching an Epiphany, because my 404 there are pictures and CSS custom page, the images with CSS are relative path (eg. / xxx xxx) is written on the page, so in order to let the site can see pictures of 404 pages, I will define the 404 pages in Nginx into an absolute path (eg. / / www. slyar. com/xxx/xxx), because the page as the external page, so will return 200 code, This results in the "soft 404" error.

It is easy to know the mistake. Define the path of a 404 page as a relative path, and for images and CSS, just use the absolute path within the page.

The correct 404 page definition method under Nginx:

1. VIM edits the Nginx configuration file, changes the vhosts configuration file separately, and changes nginx.conf configuration file directly


vim /usr/local/nginx/conf/nginx.conf

or


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

2. Specify a 404 page with a relative path


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

3. Save and exit wq and reload Nginx


/usr/local/nginx/sbin/nginx -s reload

4. Recheck 1 for a nonexistent page to see if 404 is returned


curl -I //www.slyar.com/slyar

HTTP/1.1 404 Not Found
Server: nginx/1.0.15
Date: Mon, 27 Aug 2012 08:13:56 GMT
Content-Type: text/html
Content-Length: 2110
Connection: keep-alive

5. Hide the Nginx error page and the version number on Header
Nginx will display the version number of Nginx by default on error pages, such as 403 and 404, which is very insecure. Hackers may know how to break into your server through your version number of Nginx, because certain versions of the server program may have some vulnerabilities.

The Nginx version number usually appears in two places:

1, HTTP Header, such as Server: nginx / 1. x. Information such as x expose Web server software name and version number

2. On error pages such as 403 and 404, Nginx 1.x.x and other version information will be displayed by default

Hiding Nginx version number is very simple, the authorities have given a very good solution, using server_tokens (Whether to send Nginx Nginx number in error error and Server header)

Open the configuration file Nginx.conf and add the following parameters to the http1 section


http {
... Other configuration 
server_tokens off;
}

After that, nginx-s reload overloads the Nginx configuration so that the version number of Nginx can be hidden. You can test yourself using curl.


Related articles: