nginx的rewrite规则连续跳转中的中断控制break和last

nginx的rewrite规则连续跳转中的中断控制break和last

nginx的rewrite跳转中断break和last的总结:

a).rewrite规则在location{}外,break和last作用一样,遇到break或last后,其后续的rewrite/return语句不再执行。但后续有location{}的话,还会近一步执行location{}里面的语句,当然前提是请求必须要匹配该location。

b).rewrite规则在location{}里,遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行。

c).rewrite规则在location{}里,遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头开始执行所有规则,哪个匹配执行哪个。

nginx的rewrite跳转中断break和last的示例:

1).连续多次rewrite跳转:

主配置文件http模块下添加:/usr/local/nginx/conf/nginx.conf   mkdir /usr/local/nginx/conf/vhost

include vhost/*.conf;        #添加引用一个新的目录下的配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf          #打开记录rewrite规则的日志存放路径

error_log  logs/error.log  notice;

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.1.conf

server {

    listen 80;

    server_name www.1.com;

    index index.html;

    root /data/wwwroot/www.1.com;    #定义网站根目录

    rewrite_log on;                   #打开rewrite规则的日志

    rewrite /1.html /2.html;            #跳转到网站根目录的2.html

    rewrite /2.html /3.html;            #跳转到网站根目录的3.html

}

[root@localhost ~]# echo 111 > /data/wwwroot/www.1.com/1.html

[root@localhost ~]# echo 222 > /data/wwwroot/www.1.com/2.html

[root@localhost ~]# echo 333 > /data/wwwroot/www.1.com/3.html

[root@localhost ~]# ls /data/wwwroot/www.1.com/

1.html  2.html  3.html

[root@localhost ~]# cat /etc/hosts

127.0.0.1 www.1.com

[root@localhost ~]# curl www.1.com/1.html

333

[root@localhost ~]# cat /usr/local/nginx/logs/error.log

2020/12/19 13:00:31 [notice] 20378#0: *28 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:00:31 [notice] 20378#0: *28 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:00:31 [notice] 20378#0: *28 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:00:31 [notice] 20378#0: *28 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: www.1.com

当我们请求1.html时,最终访问到的是3.html,两条rewrite规则先后执行。

2).连续多次rewrite跳转时候,使用break或last在中间中断跳转后,只跳转到中断处—(break和last在location外部)

注意:在server模块中配置的rewrite中断跳转break和last的效果是一样的,但是在location中效果不一样。

主配置文件http模块下添加:/usr/local/nginx/conf/nginx.conf   mkdir /usr/local/nginx/conf/vhost

include vhost/*.conf;        #添加引用一个新的目录下的配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf          #打开记录rewrite规则的日志存放路径

error_log  logs/error.log  notice;

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.1.conf

server {

    listen 80;

    server_name www.1.com;

    index index.html;

    root /data/wwwroot/www.1.com break或last;    #定义网站根目录

    rewrite_log on;                               #打开rewrite规则的日志

    rewrite /1.html /2.html;                        #跳转到网站根目录的2.html

    rewrite /2.html /3.html;                        #跳转到网站根目录的3.html

}

[root@localhost ~]# echo 111 > /data/wwwroot/www.1.com/1.html

[root@localhost ~]# echo 222 > /data/wwwroot/www.1.com/2.html

[root@localhost ~]# echo 333 > /data/wwwroot/www.1.com/3.html

[root@localhost ~]# ls /data/wwwroot/www.1.com/

1.html  2.html  3.html

[root@localhost ~]# cat /etc/hosts

127.0.0.1 www.1.com

[root@localhost ~]# curl www.1.com/1.html

222

[root@localhost ~]# cat /usr/local/nginx/logs/error.log

2020/12/19 13:08:48 [notice] 20915#0: *31 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:08:48 [notice] 20915#0: *31 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

当我们请求1.html时,最终访问到的是2.html,说明break在此示例中,作用是不再执行break以下的rewrite规则。

3).连续多次rewrite跳转时候,使用break或last在中间中断跳转后,只跳转到中断处—(break和last在location内部)

注意:在server模块中配置的rewrite中断跳转break和last的效果是一样的,但是在location中效果不一样。

rewrite规则在location{}里的break:

主配置文件http模块下添加:/usr/local/nginx/conf/nginx.conf   mkdir /usr/local/nginx/conf/vhost

include vhost/*.conf;        #添加引用一个新的目录下的配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf          #打开记录rewrite规则的日志存放路径

error_log  logs/error.log  notice;

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.1.conf

server{

    listen 80;

    server_name www.1.com;

root /data/wwwroot/www.1.com;

rewrite_log on;                               #打开rewrite规则的日志

    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;

    }

}

[root@localhost ~]# echo 111 > /data/wwwroot/www.1.com/1.html

[root@localhost ~]# echo 222 > /data/wwwroot/www.1.com/2.html

[root@localhost ~]# echo 333 > /data/wwwroot/www.1.com/3.html

[root@localhost ~]# echo aaa > /data/wwwroot/www.1.com/a.html

[root@localhost ~]# echo bbb > /data/wwwroot/www.1.com/b.html

[root@localhost ~]# ls /data/wwwroot/www.1.com/

1.html  2.html  3.html  a.html  b.html

[root@localhost ~]# cat /etc/hosts

127.0.0.1 www.1.com

[root@localhost ~]# curl www.1.com/1.html

222

[root@localhost ~]# cat /usr/local/nginx/logs/error.log

2020/12/19 13:37:36 [notice] 21801#0: *36 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:37:36 [notice] 21801#0: *36 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

总结:当rewrite规则在location{}里,遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行。

rewrite规则在location{}里的last:

主配置文件http模块下添加:/usr/local/nginx/conf/nginx.conf   mkdir /usr/local/nginx/conf/vhost

include vhost/*.conf;        #添加引用一个新的目录下的配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf          #打开记录rewrite规则的日志存放路径

error_log  logs/error.log  notice;

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.1.conf

server{

    listen 80;

    server_name www.1.com;

root /data/wwwroot/www.1.com;

rewrite_log on;                               #打开rewrite规则的日志

    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;

    }

}

[root@localhost ~]# echo 111 > /data/wwwroot/www.1.com/1.html

[root@localhost ~]# echo 222 > /data/wwwroot/www.1.com/2.html

[root@localhost ~]# echo 333 > /data/wwwroot/www.1.com/3.html

[root@localhost ~]# echo aaa > /data/wwwroot/www.1.com/a.html

[root@localhost ~]# echo bbb > /data/wwwroot/www.1.com/b.html

[root@localhost ~]# ls /data/wwwroot/www.1.com/

1.html  2.html  3.html  a.html  b.html

[root@localhost ~]# cat /etc/hosts

127.0.0.1 www.1.com

[root@localhost ~]# curl www.1.com/1.html

aaa

[root@localhost ~]# cat /usr/local/nginx/logs/error.log

2020/12/19 13:41:11 [notice] 21970#0: *37 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:41:11 [notice] 21970#0: *37 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:41:11 [notice] 21970#0: *37 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:41:11 [notice] 21970#0: *37 rewritten data: "/a.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:41:11 [notice] 21970#0: *37 "/1.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

2020/12/19 13:41:11 [notice] 21970#0: *37 "/2.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET /1.html HTTP/1.1", host: "www.1.com"

总结:

rewrite规则在location{}里,遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url(2.html)再次从头开始执行所有规则,哪个匹配执行哪个。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维实战课程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值