实例区分nginx location中rewrite的break与last

验证代码:    

server{

       listen 80;
       server_name www.example.com;
       location ~ /test1.txt {
            rewrite /test1.txt /test2.txt break;
            #rewrite /test1.txt /test2.txt last;
       }

       location ~ /test2.txt {
           root /var/txt/;
       }

    }


文件设置: 

       验证 rewrite /test1.txt /test2.txt break; 在 /usr/local/nginx/html 有test2.txt 内容为 /usr/local/nginx/html/test2

       验证 rewrite /test1.txt /test2.txt last; /var/txt/ 有test2.txt 内容为 /var/test2

说明:

       在使用break时,www.example.com/test1.txt  rewrite 为 www.example.com/test2.txt 不向下边的location中查找,我们会查看到 /usr/local/nginx/html/test2,说明是/usr/local/nginx/html中的test2.txt

       (last其实就相当于一个新的url,对nginx进行了一次请求,需要走一遍大多数的处理过程,最重要的是会做一次find config,提供了一个可以转到其他location的配置中处理的机会,


      在使用last时,www.example.com/test1.txt  rewrite 为 www.example.com/test2.txt 并匹配下边的location。我们会查看到   /var/test2

         (break则是在一个请求处理过程中将原来的url(包括uri和args)改写之后,在继续进行后面的处理,这个重写之后的请求始终都是在同一个location中处理。

posted @   忱康  阅读(381)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示