Nginx Rewrite规则的break和last示例
break和last各自的作用
官方解释
last:stops processing the current set of ngx_http_rewrite_module directives followed by a search for a new location matching the changed URI;
break:stops processing the current set of ngx_http_rewrite_module directives;
last: 停止当前这个请求,匹配成功后会跳出本条location,会执行其他location,会并根据rewrite匹配的规则重新发起一个请求。
break:相对last,break并不会重新发起一个请求,跳出所有location, 其他location也都不会去执行了,只是跳过当前的rewrite阶段,并执行本请求后续的执行阶段,比如在匹配成功后访问指定的目录文件
举例说明
last情况
server {
listen 80;
server_name wqy.test.com;
access_log logs/wqy-test-com/access.log;
location ^~ /aaa {
rewrite ^/aaa/(.*) /bbb/$1 last;
root /opt/tmp/wqy/test;
}
location ^~ /bbb {
return 200 "hello world\n";
}
}
测试
#curl -s http://wqy.test.com/aaa/index.html -x 127.0.0.1:80
hello world
#curl -s http://wqy.test.com/bbb/index.html -x 127.0.0.1:80
hello world
重点说明
当访问域名wqy.test.com/aaa的时候,后面跟的是/bbb/$1,再往后走发现是last规则,就停止当前规则,跳出loaction再重新执行location匹配一遍,这个时候的域名变成wqy.test.com/bbb/,重新匹配到/bbb,然后返回hello world
当访问域名wqy.test.com/bbb的时候,后面跟的是直接匹配的/bbb然后返回hello world
break情况
server {
listen 80;
server_name wqy.test.com;
access_log logs/wqy-test-com/access.log;
location ^~ /aaa {
rewrite ^/aaa/(.*) /bbb/$1 break;
root /opt/tmp/wqy/test;
}
location ^~ /bbb {
return 200 "hello world\n";
}
}
测试
#curl -s http://wqy.test.com/aaa/index.html -x 127.0.0.1:80
bbb
#curl -s http://wqy.test.com/bbb/index.html -x 127.0.0.1:80
hello world
重点说明
当访问域名wqy.test.com/aaa的时候,后面跟的是/bbb/$1,再往后走发现是break规则,不在跳出location直接就是匹配成功,读取root /opt/tmp/wqy/test/bbb文件内容bbb
当访问域名wqy.test.com/bbb的时候,后面跟的是直接匹配的/bbb然后返回hello world
如果以上还是不明白那么看下面这个例子
location /break/ {
rewrite ^/break/(.*) /test/$1 break;
return 402;
}
location /last/ {
rewrite ^/last/(.*) /test/$1 last;
return 403;
}
location /test/ {
return 508;
}
请求break:如果请求开头为/break/index.html 因为后面是break,不再进行重新匹配,就直接访问/test下的index.html文件内容,如果/test下没有文件就返回404找不到报错
请求last:如果是请求开头为/last/index.html,因为后面是last,重新跳出本location进行匹配,/test/ 直接返回508(访问last相当于直接访问/test/)
最新文章
- SQL的多表连接查询
- node-gyp rebuild 卡住?
- 动态替换fragment
- 转!!各种数据库的jdbc驱动下载及连接方式
- JAVA算术运算符、关系运算符和位运算符
- Gitlab 与 Git Windows 客户端一起使用的入门流程
- storm单机版安装配置
- Python按行读取文件
- raphael入门到精通---属性和事件篇
- ftk学习记录(脚本文章)
- 转 linux下xargs命令用法详解
- linux中安装Python3.x
- gVim编辑器 模板篇
- Java 8的用法(泛型接口,谓词链)
- Android文件各种存储路径的比较
- Sql Server XML
- fiddler学习笔记2 字段说明;移动设备、解密证书
- git 常用命令收集
- js的正则表达式编程,悬赏解决下面的问题
- 用MFC库函数AfxBeginThread()来创建线程