Learn nginx-try_files

nginx try_files
official document: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
checks whether files exist in the specified order, and responds with the first file or folder found, if all files are found Not much, an internal redirection will be performed to the last parameter.
Insert picture description here
Example, visit a picture

server {
    
    

    listen          8083;
    location        ~* .*\.(gif|jpe?g|png|gif)$ {
    
    
        root        /data/nginx/images;
        try_files   $uri  /default.jpeg  =404;


    }
    location        / {
    
    

        root       /data/nginx/images;

    }

}

There are the following pictures in the root directory
Insert picture description here

When accessing http://192.168.243.129:8083/dog.png, the original uri used first, find it under root, and it will respond to the picture
Insert picture description here
if it exists. When accessing http://192.168.243.129:8083/dog1.png , use the original uri first. If the uri cannot find the picture, try to respond with the next uri. If the next uri does not exist, it has not been found until a file that can be responded to is found. If the last digit is the response code, the request will be processed directly with the response code.
Insert picture description here

If the file of the second uri in root does not exist, then directly respond to the last uri or response code, and directly respond to 404 in the configuration of our example,

Modify the default.jpeg under root to xxx.jpeg,
Insert picture description here
then 404 will be returned according to the try_files rule
Insert picture description here

Sometimes there is a requirement that no responseable file can be found in all the parameters, and the request is forwarded to the background using the named location, and the back-end server responds. The configuration is as follows:

server {
    
    

    listen          8083;
    location        ~* .*\.(gif|jpe?g|png|gif)$ {
    
    
        root        /data/nginx/images;
        try_files   $uri  /default.jpeg  @fallback;


    }
    location        / {
    
    

        root       /data/nginx/images;

    }
    location     @fallback   {
    
    

         proxy_pass http://192.168.243.129:8081;

    }

}

When the 8083 server cannot be found, this configuration will directly forward the uri to the 8081 end, becoming http://192.168.243.129:8081/dog1.png and find dog1.png on the 8081 end. In response to the request, if it does not exist and there is no try_files, then directly respond to 404.
Insert picture description here
Of course, for fault tolerance, we should also set try_files on the backend server

The configuration is as follows:
back-end configuration
server { listen 8081; root /data/nginx/domain3; try_files $uri /home_page.html;


}
First we requested http://192.168.243.129:8083/dog2.png, the picture does not exist under 8083 root, and default.jpeg is forwarded to http://192.168.243.129:8081 according to the configuration request uri /dog2.png, but when 8081 cannot find dog2.png based on the original uri, it will try the next (that is, the last) uri http://192.168.243.129:8081/home_page.html, but also under the root of 8081 If /home_page.html does not exist, then the backend $uri = /home_page.html request becomes http://192.168.243.129:8081/home_page.html Can’t find /home_page.html, keep looping, so in general The next last parameter must be followed by the response code.
Insert picture description hereserver { listen 8081; root /data/nginx/domain3; try_files $uri /home_page.html =404;


}
Insert picture description here

Guess you like

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