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)再次从头开始执行所有规则,哪个匹配执行哪个。