nginx-rewrite之break、last、return顺序关系

无 flag
配置一

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;

    }



}

访问http://192.168.243.129:8086/1.html 执行顺序为

首先匹配location 匹配到

 location     /  {

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

    }

然后根据第一个rewrite 将uri /1.html 改写成了 /2.html,但是未重新发起请求 而是在当前location中查找是否有关于/2.html的 rewrite,查找到第二个rewrite将uri /2.html 改写成/3.html,再去查找当前locaiton中是否还有/3.html的 重写规则,发现并没有,此时URL=http://192.168.243.129:8086/3.html,
再次去请求,匹配到了

 location    /3.html {

        rewrite   /3.html   /b.html;

    }

进入该location中 有/3.html的重写规则 将uri 重写为/b.html, 这个location中没有/b.html 的重写规则
URL=http://192.168.243.129:8086/b.html
再次请求,匹配到

 location     /  {

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

    }

但是该location中没有/b.html的匹配规则,所以直接响应b.html
nginx-rewrite之break、last、return顺序关系
flag为break,停止运行rewrite_model

配置

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;

    }



}

访问http://192.168.243.129:8086/1.html,匹配

location     /  {

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

    }

执行到第一个rewrite规则 将1.html 重写为/2.html,break 直接终止了当前location的rewirte 和其他location的匹配,返回/2.html
nginx-rewrite之break、last、return顺序关系
falg为last,停止当前location 的rewrite 匹配,还可以继续匹配其他location 的rewrite规则

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;

    }



}

访问http://192.168.243.129:8086/1.html 匹配到

location     /  {

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

    }

在该location中匹配到第一条rewrite规则 将uri /1.html 改写成/2.html last指令直接将URL=http://192.168.243.129:8086/2.html
去请求,匹配到

location    /2.html {

        rewrite   /2.html   /a.html;


    }

又将uri从2.html 改写成/a.html URL=http://192.168.243.129:8086/a.html,去请求,匹配到

location     /  {

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

    }

在该location中没有匹配/a.html 的重写规则 则直接返回a.html
但是修改location的优先级,比如在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;

    }



}

访问http://192.168.243.129:8086/1.html 匹配到

location     ~ /  {

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

    }

在此location中匹配到了rewrite规则 将/1.html 改写为/2.html ,URL=http://192.168.243.129:8086/2.html,去请求,由于该location为正则匹配 优先级高于其他的location 字符串匹配 所以还是匹配当前的location,在该location中/2.html 改写为/3.html
找不到/3.html的rewirte规则后 ,直接返回3.html 。

last 、break return执行顺序 break在前 返回2.html,当return在前时 返回"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;
 }
}
上一篇:Nginx处理请求的11个阶段


下一篇:php-htaccess在iis上不起作用