• Nginx中的break和last


    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再次从头开始执行所有规则,哪个匹配执行哪个。
  • 相关阅读:
    在字符串中,找出第一个不重复的字符
    servlet/filter/listener/interceptor区别与联系
    Java基础--泛型
    Java基础--访问权限控制符
    Comparable与Comparator的区别
    表示层设计模式:Intercepting Filter(截取筛选器)模式
    Java多线程:线程同步与关键字synchronized
    Java多态-如何理解父类引用指向子类对象
    android 垂直 SeekBar 源代码(VerticalSeekBar)[转]
    回调接口
  • 原文地址:https://www.cnblogs.com/xuliangwei/p/11568223.html
Copyright © 2020-2023  润新知