The order of break, last, return of nginx-rewrite

No flag
configuration one

server {
    
    
    listen       8086;
    rewrite_log  on;
    error_log    /var/log/nginx/rw_error.log  notice;
    root         /data/nginx/domain7;
    location     /  {
    
    

        rewrite   /1.html  /2.html;
        rewrite   /2.html  /3.html;

    }

    location    /2.html {
    
    

        rewrite   /2.html   /a.html;


    }

    location    /3.html {
    
    

        rewrite   /3.html   /b.html;

    }



}

Visit http://192.168.243.129:8086/1.html The execution order is

First match location to match

 location     /  {
    
    

        rewrite   /1.html  /2.html;
        rewrite   /2.html  /3.html;

    }

Then according to the first rewrite, the uri /1.html was rewritten to /2.html, but the request was not re-initiated, but to find out if there is a rewrite about /2.html in the current location, and the second rewrite was found to be the uri Rewrite /2.html into /3.html, and then check whether there is a rewrite rule of /3.html in the current locaiton, and found that there is none, at this time URL=http://192.168.243.129:8086/3.html ,
Go to the request again, it matches

 location    /3.html {
    
    

        rewrite   /3.html   /b.html;

    }

There is a rewrite rule of /3.html in the location to rewrite the uri to /b.html, and there is no rewrite rule of /b.html in this location
URL=http://192.168.243.129:8086/b.html
Request again, match

 location     /  {
    
    

        rewrite   /1.html  /2.html;
        rewrite   /2.html  /3.html;

    }

But there is no matching rule for /b.html in this location, so directly respond to the b.html
Insert picture description here
flag as break and stop running rewrite_model

Configuration

server {
    
    
    listen       8086;
    rewrite_log  on;
    error_log    /var/log/nginx/rw_error.log  notice;
    root         /data/nginx/domain7;
    location     /  {
    
    

        rewrite   /1.html  /2.html break;
        rewrite   /2.html  /3.html;

    }

    location    /2.html {
    
    

        rewrite   /2.html   /a.html;


    }

    location    /3.html {
    
    

        rewrite   /3.html   /b.html;

    }



}

Visit http://192.168.243.129:8086/1.html, match

location     /  {
    
    

        rewrite   /1.html  /2.html break;
        rewrite   /2.html  /3.html;

    }

When the first rewrite rule is executed, 1.html is rewritten to /2.html, break directly terminates the match between the
Insert picture description here
rewirt of the current location and other locations, returns /2.html falg as last, and stops the rewrite match of the current location. Can continue to match the rewrite rules of other locations

server {
    
    
    listen       8086;
    rewrite_log  on;
    error_log    /var/log/nginx/rw_error.log  notice;
    root         /data/nginx/domain7;
    location     /  {
    
    

        rewrite   /1.html  /2.html last;
        rewrite   /2.html  /3.html;

    }

    location    /2.html {
    
    

        rewrite   /2.html   /a.html;


    }

    location    /3.html {
    
    

        rewrite   /3.html   /b.html;

    }



}

Visit http://192.168.243.129:8086/1.html to match

location     /  {
    
    

        rewrite   /1.html  /2.html last;
        rewrite   /2.html  /3.html;

    }

Match the first rewrite rule in this location, rewrite uri /1.html to /2.html last command, directly
request URL=http://192.168.243.129:8086/2.html , and match

location    /2.html {
    
    

        rewrite   /2.html   /a.html;


    }

Rewrite the uri from 2.html to /a.html URL=http://192.168.243.129:8086/a.html, go to the request, and match

location     /  {
    
    

        rewrite   /1.html  /2.html last;
        rewrite   /2.html  /3.html;

    }

If there is no rewriting rule matching /a.html in the location, it will directly return to a.html
but modify the priority of location, such as enabling regular in llocation

server {
    
    
    listen       8086;
    rewrite_log  on;
    error_log    /var/log/nginx/rw_error.log  notice;
    root         /data/nginx/domain7;
    location     ~ /  {
    
    

        
        rewrite   /1.html  /2.html last;
        rewrite   /2.html  /3.html;

    }

    location    /2.html {
    
    

        rewrite   /2.html   /a.html;


    }

    location    /3.html {
    
    

        rewrite   /3.html   /b.html;

    }



}

Visit http://192.168.243.129:8086/1.html to match

location     ~ /  {
    
    

        
        rewrite   /1.html  /2.html last;
        rewrite   /2.html  /3.html;

    }

In this location, the rewrite rule is matched. Rewrite /1.html to /2.html, URL=http://192.168.243.129:8086/2.html, go to the request, because the location is regular, the matching priority is higher than others The location string matches so it still matches the current location. In the location, /2.html is rewritten as
/3.html and the rewirte rule of /3.html is not found, and 3.html is returned directly.

The execution order of last and break return, break first returns 2.html, when return first returns "It's OK!"

server {
    
    
 listen 80 default_server;
 server_name www.a.com a.com;
 charset utf-8;
 root /data/nginx/a;
 rewrite_log on;
 error_log /var/log/nginx/a-error.log info;
 location / {
    
    
     rewrite /1.html /2.html break;
     rewrite /2.html /3.html;
     return 200 "It's OK!";
 }
 location /2.html {
    
    
     rewrite /2.html /a.html;
 }
 location /3.html {
    
    
     rewrite /3.html /b.html;
 }
}

Guess you like

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