博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
标志位 last break
阅读量:7045 次
发布时间:2019-06-28

本文共 2828 字,大约阅读时间需要 9 分钟。

 

last-完成rewrite指令的处理,之后搜索对应的URI和location;break-完成rewrite指令的外理
[root@web01 app]# cat /app/server/nginx/conf/vhosts/default.confserver {    listen 80 default_server;    server_name localhost;    root /app/www;    location / {        root /app/www/html;    index index.html index.htm;    rewrite "/x/t.html" /y/t.html break;    }     location /y/ {         root /app/www/html/other;    }}
[root@web01 other]# tree /app/www/html/ /app/www/html/ ├── index.html ├── other │   └── y │       └── t.html └── y     └── t.html 3 directories, 3 files [root@web01 other]# cat /app/www/html/y/t.html yyy [root@web01 other]# cat /app/www/html/other/y/t.html other----->last [root@web01 other]#
[root@web01 app]# curl http://192.168.1.24/x/t.htmlyyy

当请求/x/t.html,符合rewrite规则,所以进行调整,调整的地址为/y/t.html,由于使用的flag是break,所以在 “location /”中进行跳转,结果是/app/www/html/y/t.html。但如果flag为last,那么/y/t.html将在"server"标签中重新执 行,由于存在一个“location /y/”,所以跳转被重新指定到“location /y/”标签,所以这个时候的结果为/app/www/html/other/y/t.html

如下测式:

[root@web01 other]# cat /app/www/html/other/y/t.html other----->last[root@web01 other]# cat /app/server/nginx/conf/vhosts/default.confserver {    listen 80 default_server;    server_name localhost;    root /app/www;    location / {        root /app/www/html;    index index.html index.htm;    #rewrite "/x/t.html" /y/t.html break;    rewrite "/x/t.html" /y/t.html last;#注意这里已经改成last    }     location /y/ {         root /app/www/html/other;    }}[root@web01 other]# curl http://192.168.1.24/x/t.htmlother----->last

 注意:使用last,如果配置不当,可能引起死循环。

[root@web01 www]# cat /app/server/nginx/conf/vhosts/default.confserver {    listen 80 default_server;    server_name localhost;    root /app/www;    location / {    rewrite /last/ /q.html last;#是相对于root /app/www 所以真实的访问路径是 /app/www/q.html    rewrite /break/ /q.html break;    }    location =/q.html {    return 400;        }    location ~ .*\.(php|php5)?$    {       #fastcgi_pass  unix:/tmp/php-cgi.sock;        fastcgi_pass  127.0.0.1:9000;    fastcgi_index index.php;    include fastcgi.conf;    }    access_log  /app/log/nginx/access/default.log;}[root@web01 www]# tree /app/www//app/www/├── index.php└── q.html0 directories, 2 files[root@web01 www]# cat /app/www/q.html clnking@163.com[root@web01 www]# curl 192.168.1.24/last/q.html400 Bad Request

400 Bad Request


nginx/1.4.4
[root@web01 www]# curl 192.168.1.24/break/q.htmlclnking@163.com

 

  • last一般写在server和if中,而break一般使用在location中
  • last不终止重写后的url匹配,即新的url会再从server走一遍匹配流程,而break终止重写后的匹配
  • break和last都能组织继续执行后面的rewrite指令

location里一旦返回break则直接生效并停止后续的匹配location

server {    location / {        rewrite /last/ /q.html last;        rewrite /break/ /q.html break;    }    location = /q.html {        return 400;    }}
  • 访问/last/时重写到/q.html,然后使用新的uri再匹配,正好匹配到locatoin = /q.html然后返回了400
  • 访问/break时重写到/q.html,由于返回了break,则直接停止了.

转载地址:http://vmzol.baihongyu.com/

你可能感兴趣的文章
给 Android 开发者的一点福利:免费模拟面试
查看>>
LeetCode Animation 题目图解汇总(持续更新中...)
查看>>
来个简单的事件委托 冒个泡
查看>>
设计模式系列·Facade模式之MVC的烦恼
查看>>
hadoop入门操作
查看>>
Node模块--child_process
查看>>
ATOM如何删除window边框,并且自定义样式
查看>>
[转]MD5(2)-破解MD5之我见
查看>>
mianxiangduixiang
查看>>
CSS自定义变量属性——像less,sass那样在css中使用变量(译)
查看>>
在 Android 上离线导览模型
查看>>
深入css之去除inline-block元素之间的多余间隙
查看>>
关于cronjob的用法
查看>>
对于fork()用法的初步探讨
查看>>
Javascript 数组循环遍历之forEach
查看>>
HTML & CSS之小白初入江湖
查看>>
写一个简单的webserver
查看>>
通过 InnoSetup 美化安装界面
查看>>
一次不怎么愉快的滴滴面试经历
查看>>
Android的资源管理器的创建过程
查看>>