Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to realize 404 pages in Nginx

Shulou Source: shulou.com Published: 2022-06-01 15:02:28 10月16日 Update

Today, I will talk to you about how to achieve 404 pages in Nginx, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

* species: 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 80; server_name www.test.com; root / var/www/test; index index.html index.htm; location / {} # defines the error page number and forwards it there if the corresponding error page number occurs. Error_page 404 403 500 502 503 504 / 404. Html; # inherits the above location. Location = / 404.html {# the directory path where the error page is placed. Root / usr/share/nginx/html;}}

Second: the error page of the 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.201virtual 7777 weight=20 max_fails=2 fail_timeout=30s; ip_hash;} server {listen 80; server_name 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; Nginx to 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 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.html; location = / 404.html {root / usr/share/nginx/html;}

Specify an url address:

Error_page 404 / 404.html; error_page 404 = http://www.test.com/error.html; after reading the above, do you have any further understanding of how to implement 404 pages in Nginx? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Pages errors content variables parameters customer client faceted configuration code key function background address situation time more status directory knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS vpn Huawei Shulou Technology