Get the App
SLTechnology News&Howtos  ›  Development  › 

How to customize the dedicated 404 page of Nginx

Shulou Source: shulou.com Published: 2022-06-01 16:55:06 10月03日 Update

Today, I would like to share with you how Nginx customizes the relevant knowledge points of the dedicated 404 page, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

The first: Nginx's own error page

Nginx visits a static html page, and when the page is not available, Nginx throws 404, so how do you return it to the client?

Take a look at the configuration below, in which case you don't need to modify any parameters to achieve this function.

Server {listen 80th serverpage name www.test.com;root / var/www/test;index index.html index.htm;location / {} # defines the error page number, and if the corresponding error page number occurs, forward it there. Error_page 404, 403, 500, 502, 503, 504 / 404. Htmlwitt # inherits the above location. Location = / 404.html {# the directory path where the error page is placed. Root / usr/share/nginx/html;}} the second kind: error page of reverse proxy

If the backend Tomcat process throws a 404 error, and you want to call Nginx to feedback this status to the client or redirect to a connection, configure as follows:

Upstream www {server 192.168.1.201 server 7777 weight=20 max_fails=2 fail_timeout=30s;ip_hash;} server {listen 80th serveraccounname www.test.com;root / var/www/test;index index.html index.htm;location / {if ($request_uri ~ *'^ / $') {rewrite. * http://www.test.com/index.html redirect } # key parameters: only when this variable is enabled can we customize the error page. When the backend returns 404 http://www;proxy_set_header Nginx intercept error definition error page proxy_intercept_errors on;proxy_pass http://www;proxy_set_header HOST $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-FOR $proxy_add_x_forwarded_for;} error_page 404 / 404.html Location = / 404.html {root / usr/share/nginx/html;}} third: Nginx parses the error page of the php code

If the backend is parsed by php, you need to add a variable

Just add a variable fastcgi_intercept_errors on to the http section.

Specify an error page:

Error_page 404 / 404. Usr/share/nginx/html; location = / 404.html {root / usr/share/nginx/html;} specify a url address: error_page 404 / 404. Htmlownership page 404 = http://www.test.com/error.html; is all the content of the article "how to customize the dedicated 404 page of Nginx". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Page error knowledge article variable facet content parameter customer client configuration different big code key function background address most of it Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Huawei MySQL Linux