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/)

最新文章

  1. SQL的多表连接查询
  2. node-gyp rebuild 卡住?
  3. 动态替换fragment
  4. 转!!各种数据库的jdbc驱动下载及连接方式
  5. JAVA算术运算符、关系运算符和位运算符
  6. Gitlab 与 Git Windows 客户端一起使用的入门流程
  7. storm单机版安装配置
  8. Python按行读取文件
  9. raphael入门到精通---属性和事件篇
  10. ftk学习记录(脚本文章)
  11. 转 linux下xargs命令用法详解
  12. linux中安装Python3.x
  13. gVim编辑器 模板篇
  14. Java 8的用法(泛型接口,谓词链)
  15. Android文件各种存储路径的比较
  16. Sql Server XML
  17. fiddler学习笔记2 字段说明;移动设备、解密证书
  18. git 常用命令收集
  19. js的正则表达式编程,悬赏解决下面的问题
  20. 用MFC库函数AfxBeginThread()来创建线程

热门文章

  1. 极验验证码破解之selenium
  2. 【原创】面试官问我G1回收器怎么知道你是什么时候的垃圾?
  3. C++扬帆远航——17(递归函数求阶乘)
  4. 10分钟进阶SpringBoot - 05. 数据访问之JDBC(附加源码分析+代码下载)
  5. java单链表的实现自己动手写一个单链表
  6. 大厂面试题:集群部署时的分布式 session 如何实现?
  7. GPUImage学习总结
  8. 一起了解 .Net Foundation 项目 No.13
  9. ES6 常用知识点总结
  10. IoT设备实践丨如果你也在树莓派上部署了k3s,你也许需要这篇文章