rewrite中的break和last

两个指令用法相同,但含义不同,需要放到rewrite规则的末尾,用来控制重写后的链接是否继续被nginx配置执行(主要是rewrite、return指令)。示例1(连续两条rewrite规则):
server{listen 80; server_name test.com;root /tmp/123.com;rewrite /1.html /2.html ;rewrite /2.html /3.html ;}
当我们请求1.html时,最终访问到的是3.html,两条rewrite规则先后执行。
break和last在location {}外部
格式:rewrite xxxxx  break;示例2(增加break):
server{listen 80; server_name test.com;root /tmp/123.com;rewrite /1.html /2.html break;rewrite /2.html /3.html;
}
当我们请求1.html时,最终访问到的是2.html
说明break在此示例中,作用是不再执行break以下的rewrite规则。但,当配置文件中有location时,它还会去执行location{}段的配置(请求要匹配该location)。示例3(break后面还有location段):
server{listen 80; server_name test.com;root /tmp/123.com;rewrite /1.html /2.html break;rewrite /2.html /3.html;location /2.html {return 403;}
}
当请求1.html时,最终会返回403状态码,说明它去匹配了break后面的location{}配置。以上2个示例中,可以把break替换为last,它们两者起到的效果一模一样。
当break和last在location{}里面
示例4(什么都不加):
server{listen 80; server_name test.com;root /tmp/123.com;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;}
}
当请求/1.html,最终将会访问/b.html,连续执行location /下的两次rewrite,跳转到了/3.html,然后又匹配location /3.html示例5(增加break):
server{listen 80; server_name test.com;root /tmp/123.com;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;}
}
当请求/1.html,最终会访问/2.html
在location{}内部,遇到break,本location{}内以及后面的所有location{}内的所有指令都不再执行。示例6(增加last):
server{listen 80; server_name test.com;root /tmp/123.com;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;}
}
当请求/1.html,最终会访问/a.html
在location{}内部,遇到last,本location{}内后续指令不再执行,而重写后的url再次从头开始,从头到尾匹配一遍规则。
结论
  • 当rewrite规则在location{}外,break和last作用一样,遇到break或last后,其后续的rewrite/return语句不再执行。但后续有location{}的话,还会近一步执行location{}里面的语句,当然前提是请求必须要匹配该location。
  • 当rewrite规则在location{}里,遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行。
  • 当rewrite规则在location{}里,遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头开始执行所有规则,哪个匹配执行哪个。

转载于:https://www.cnblogs.com/xuliangwei/p/11568223.html

Nginx中的break和last相关推荐

  1. Nginx中last和break redirect和permanent区别和联系

    参考:https://www.phpmianshi.com/?id=98 一.last & break (1)last 和 break 当出现在location 之外时,两者的作用是一致的没有 ...

  2. Nginx中的rewrite指令(break,last,redirect,permanent)

    转载自:Nginx中的rewrite指令(break,last,redirect,permanent) rewite 在server块下,会优先执行rewrite部分,然后才会去匹配location块 ...

  3. WAF(NGINX)中502和504的区别

    0x00 前言 华为云WAF使用最好的服务器和带宽为客户提供反向代理和安全检测服务,但是在使用过程中,个别用户的请求出现了502或者504.因此我们团队也经常受到用户的反馈,这里可以大致和大家澄清一下 ...

  4. Nginx中常用的指令配置详解

    rewrite指令的作用:是做一些访问匹配规则的判断,从而实现相应的操作. location / { proxy_pass http://192.168.1.5 if (-d $request_fil ...

  5. Nginx中的location规则与rewrite重写(详解)

    内容预知 1.Nginx中location与rewrite 1.1 location与rewrite常用的正则表达式 1.2  location与rewrite的联系和区别 2.location的匹配 ...

  6. Nginx中rewrite的用法详解

    文章目录 一.rewrite简介 1.1 rewrite跳转场景 1.2 rewrite跳转实现过程 1.3 rewrite实际场景 1.4 nginx正则表达式 二.rewrite命令 2.1 re ...

  7. web服务之Nginx中的location匹配与rewrite重写跳转

    文章目录 常见的Nginx正则表达式 location location分为三类 location常用的匹配规则 location 优先级 location 示例说明 三个匹配规则定义 rewrite ...

  8. centos 7.6——Nginx中rewrite模块应用(location)——基于域名的跳转等

    centos 7.6--Nginx中rewrite模块应用(location)--基于域名的跳转等 文章目录 centos 7.6--Nginx中rewrite模块应用(location)--基于域名 ...

  9. nginx中许多if判断的写法

    1.首先我们回顾一下nginx中location的相关知识 1)location的匹配指令: ~ #波浪线表示执行一个正则匹配,区分大小写 ~* #表示执行一个正则匹配,不区分大小写 ^~ #^~表示 ...

最新文章

  1. 基于Android设备的Kali Linux渗透测试教程第1章渗透测试
  2. python大神-国内某Python大神自创完整版,系统性学习Python
  3. iOS 中捕获程序崩溃日志
  4. wordpress后台无法登录问题
  5. linux搭建vsftp服务器_Linux配置VSFTP服务器的方法
  6. Java 7 对ArrayList和HashMap的性能的提升
  7. Android手机通讯录解析
  8. 注册事件的两种方式(传统注册事件、方法监听注册事件)
  9. 做网站买主机还是服务器,做网站买主机还是服务器
  10. 联想计算机BIOS密码忘记了,联想笔记本CMOS(BIOS)密码清除
  11. 基于php+mysql的大学生四六级英语考试报名成绩管理
  12. 干货|手把手教你写一个串口调试助手
  13. 已安装各个模块,程序仍报错:ModuleNotFoundError: No module named 'numpy'
  14. qt creator在高分辨率笔记本上控件运行显示不全的问题解决方法
  15. 测量的基准面和基准线
  16. QQ2005正式版将于27日发布!(转)
  17. 7-1 C0216:输入矩形的长和宽,输出周长和面积
  18. 为Arduino IDE安装添加库
  19. 用计算机弹莫问归期数字,莫问归期 - 在线打字测试(dazi.kukuw.com)
  20. 安卓应用移植鸿蒙(二):移植安卓的字符串等资源文件到鸿蒙应用

热门文章

  1. bootstrap table 表格支持shirt 多选_bootstrap-table 表格行内编辑实现
  2. c++ 线程软件看门狗_装配生产线MES系统软件
  3. Druid 配置及内置监控,Web页面查看监控内容 【我改】
  4. 【JZOJ3885】【长郡NOIP2014模拟10.22】搞笑的代码
  5. linux下(ubuntu)反删除(误删恢复)与回收站制作
  6. (转)WP7 开发学习(2):在WP7中使用网络请求
  7. 解决IE下不支持placeholder属性可以根据自己的需要去扩展
  8. react封装函数_React 模式-将函数作为 children 传入和 render prop - 极客教程
  9. git 代码回滚_git代码版本管理(1)——git版本回滚
  10. c post请求网页_Python使用urllib2抓取网页