nginx-rewrite之break、last、return顺序关系
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了nginx-rewrite之break、last、return顺序关系,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3770字,纯文字阅读大概需要6分钟。
内容图文
![nginx-rewrite之break、last、return顺序关系](/upload/InfoBanner/zyjiaocheng/926/8850bb3d1876482fb753bcc2aa6aa034.jpg)
无 flag
配置一
server {
listen 8086;
rewrite_log on;
error_log /var/log/nginx/rw_error.log notice;
root /data/nginx/domain7;
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;
}
}
访问http://192.168.243.129:8086/1.html 执行顺序为
首先匹配location 匹配到
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
然后根据第一个rewrite 将uri /1.html 改写成了 /2.html,但是未重新发起请求 而是在当前location中查找是否有关于/2.html的 rewrite,查找到第二个rewrite将uri /2.html 改写成/3.html,再去查找当前locaiton中是否还有/3.html的 重写规则,发现并没有,此时URL=http://192.168.243.129:8086/3.html,
再次去请求,匹配到了
location /3.html {
rewrite /3.html /b.html;
}
进入该location中 有/3.html的重写规则 将uri 重写为/b.html, 这个location中没有/b.html 的重写规则
URL=http://192.168.243.129:8086/b.html
再次请求,匹配到
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
但是该location中没有/b.html的匹配规则,所以直接响应b.html
flag为break,停止运行rewrite_model
配置
server {
listen 8086;
rewrite_log on;
error_log /var/log/nginx/rw_error.log notice;
root /data/nginx/domain7;
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;
}
}
访问http://192.168.243.129:8086/1.html,匹配
location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
执行到第一个rewrite规则 将1.html 重写为/2.html,break 直接终止了当前location的rewirte 和其他location的匹配,返回/2.html
falg为last,停止当前location 的rewrite 匹配,还可以继续匹配其他location 的rewrite规则
server {
listen 8086;
rewrite_log on;
error_log /var/log/nginx/rw_error.log notice;
root /data/nginx/domain7;
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;
}
}
访问http://192.168.243.129:8086/1.html 匹配到
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
在该location中匹配到第一条rewrite规则 将uri /1.html 改写成/2.html last指令直接将URL=http://192.168.243.129:8086/2.html
去请求,匹配到
location /2.html {
rewrite /2.html /a.html;
}
又将uri从2.html 改写成/a.html URL=http://192.168.243.129:8086/a.html,去请求,匹配到
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
在该location中没有匹配/a.html 的重写规则 则直接返回a.html
但是修改location的优先级,比如在llocation 中启用正则
server {
listen 8086;
rewrite_log on;
error_log /var/log/nginx/rw_error.log notice;
root /data/nginx/domain7;
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;
}
}
访问http://192.168.243.129:8086/1.html 匹配到
location ~ / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
在此location中匹配到了rewrite规则 将/1.html 改写为/2.html ,URL=http://192.168.243.129:8086/2.html,去请求,由于该location为正则匹配 优先级高于其他的location 字符串匹配 所以还是匹配当前的location,在该location中/2.html 改写为/3.html
找不到/3.html的rewirte规则后 ,直接返回3.html 。
last 、break return执行顺序 break在前 返回2.html,当return在前时 返回"It’s OK!"
server {
listen 80 default_server;
server_name www.a.com a.com;
charset utf-8;
root /data/nginx/a;
rewrite_log on;
error_log /var/log/nginx/a-error.log info;
location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
return 200 "It's OK!";
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
内容总结
以上是互联网集市为您收集整理的nginx-rewrite之break、last、return顺序关系全部内容,希望文章能够帮你解决nginx-rewrite之break、last、return顺序关系所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。