nginx-error_page

nginx error_page
returns the corresponding page according to the error response code, or specifies the response code
Insert picture description here

error_page + notfound_uri
示例:

server {
    
    
    listen         80;
    server_name    www.a.com;
    root           /data/nginx/domain1;
    location      ~ /images/  {
    
    

        root          /data/nginx/test1;
        error_page    404   /notfound.html;

    }
    location       /   {
    
    

        root           /data/nginx/test;
        error_page     404  /notfound.html;


    }
}

First create notfound.html in the two root directories

echo "/data/nginx/images/notfound.html" > /data/nginx/images/notfound.html
echo "/data/nginx/test1/notfound.html"  > /data/nginx/test1/notfound.html

Then visit the browser to match the a.html
www.a.com/a.html that does not exist in the two roots.

location       /   {
    
    

        root           /data/nginx/test;
        error_page     404  /notfound.html;


    }

responseInsert picture description here

When the browser access
http://www.a.com/images/a.html
match to

 location      ~ /images/  {
    
    

        root          /data/nginx/test1;
        error_page    404   /notfound.html;

    }

Response It
Insert picture description here
can be seen that although the localtion is not matched, it responds to the same notfound.html. The reason is that when you visit http://www.a.com/images/a.html uri=/images/a.html , it returns 404 at this time uri=/notfound.html complete URL=http://www.a.com /notfound.html so it will match

location       /   {
    
    

        root           /data/nginx/test;
        error_page     404  /notfound.html;


    }

Then respond with /data/nginx/test/notfound.html.

In order to prevent the browser from intercepting the error page, you can specify a response code such as 404 and 200 in the two locations.

location       /   {
    
    

        root           /data/nginx/test;
        error_page     404  =200 /notfound.html;


    }

As shown
Insert picture description here

Insert picture description here

error_page + location @

When part of the service is upgraded and an error is reported when accessing a certain page, in order not to affect the customer experience, the page is proxied to the unupdated server.
The configuration is as follows:

server {
    
    
    listen          8080;
    error_log       /var/log/nginx/domain2_error.log;
    location       /  {
    
    
        root        /data/nginx/domain2;
        error_page  404  @fallback;

    }
    location      @fallback {
    
    

        proxy_pass http://192.168.243.129:8081;

    }
}

Preparatory work, create the root directory of the virtual host 192.168.243.129:8080, write index.html to test the website is normal.
Create the virtual host 192.168.243.129:8080 and its root directory, write index.html and a.html.
As shown in the figure below, it
represents the upgraded server
Insert picture description here

On behalf of the server that has not been upgraded, the
Insert picture description here
original a.html content
Insert picture description here

Visit http://192.168.243.129:8080/a.html in the browser.
Insert picture description here
As can be seen from the picture, although accessing http://192.168.243.129:8080/a.html reports an error 404, the page jumps to 192.168.243.129 :8081/a.html responded to the correct page.
In order to prevent customers from perceiving this error, you can keep the error code consistent with the back-end server and configure it as

server {
    
    
    listen          8080;
    error_log       /var/log/nginx/domain2_error.log;
    location       /  {
    
    
        root        /data/nginx/domain2;
        error_page  404 =  @fallback;

    }
    location      @fallback {
    
    

        proxy_pass http://192.168.243.129:8081;

    }
}
~

The effect is as shown in the figure,
Insert picture description here
the error is defined to the external website

When the access error is reported, the page will be redirected to the external network, and the
configuration is as follows:

server {
    
    

    listen                8082;
    location              /   {
    
    

        root              /data/nginx/domain4;
        error_page        404   http://www.baidu.com;

    }



}

Visit http://192.168.243.129:8082/a.html

Insert picture description hereOnly the modification of the specified response code 302 and 301 is supported, and the modification of other response codes will not take effect.
For example:

server {
    
    

    listen                8082;
    location              /   {
    
    

        root              /data/nginx/domain4;
        error_page        404 =200  http://www.baidu.com;

    }



}

Go to visit again, the response code is still 302
Insert picture description here
if specified as 301
Insert picture description here

200: Health response code
302: Temporary redirection
301: Permanent redirection
304: The cache of the browser has not changed
404: Page not found
403: No permission, generally nginx users do not have permission to access files

Guess you like

Origin blog.csdn.net/weixin_45937255/article/details/115324250