zoukankan      html  css  js  c++  java
  • Nginx Rewrite规则的break和last示例

    break和last各自的作用

    官方解释
    last:stops processing the current set of ngx_http_rewrite_module directives followed by a search for a new location matching the changed URI;
    break:stops processing the current set of ngx_http_rewrite_module directives;
    last: 停止当前这个请求,匹配成功后会跳出本条location,会执行其他location,会并根据rewrite匹配的规则重新发起一个请求。
    break:相对last,break并不会重新发起一个请求,跳出所有location, 其他location也都不会去执行了,只是跳过当前的rewrite阶段,并执行本请求后续的执行阶段,比如在匹配成功后访问指定的目录文件

    举例说明
    last情况

    server {
        listen 80;
        server_name wqy.test.com;
        access_log logs/wqy-test-com/access.log;
        location ^~ /aaa {
            rewrite ^/aaa/(.*) /bbb/$1 last;
            root /opt/tmp/wqy/test;
        }
        location ^~ /bbb {
            return 200 "hello world
    ";
        }
    }
    

    测试

    #curl -s http://wqy.test.com/aaa/index.html -x 127.0.0.1:80
    hello world
    #curl -s http://wqy.test.com/bbb/index.html -x 127.0.0.1:80
    hello world
    

    重点说明
    当访问域名wqy.test.com/aaa的时候,后面跟的是/bbb/$1,再往后走发现是last规则,就停止当前规则,跳出loaction再重新执行location匹配一遍,这个时候的域名变成wqy.test.com/bbb/,重新匹配到/bbb,然后返回hello world
    当访问域名wqy.test.com/bbb的时候,后面跟的是直接匹配的/bbb然后返回hello world

    break情况

    server {
        listen 80;
        server_name wqy.test.com;
        access_log logs/wqy-test-com/access.log;
        location ^~ /aaa {
            rewrite ^/aaa/(.*) /bbb/$1 break;
            root /opt/tmp/wqy/test;
        }
        location ^~ /bbb {
            return 200 "hello world
    ";
        }
    }
    

    测试

    #curl -s http://wqy.test.com/aaa/index.html -x 127.0.0.1:80
    bbb
    #curl -s http://wqy.test.com/bbb/index.html -x 127.0.0.1:80
    hello world
    

    重点说明
    当访问域名wqy.test.com/aaa的时候,后面跟的是/bbb/$1,再往后走发现是break规则,不在跳出location直接就是匹配成功,读取root /opt/tmp/wqy/test/bbb文件内容bbb
    当访问域名wqy.test.com/bbb的时候,后面跟的是直接匹配的/bbb然后返回hello world

    如果以上还是不明白那么看下面这个例子

    location /break/ {
    		rewrite ^/break/(.*) /test/$1 break;
    		return 402;
    }
    location /last/ {
    		rewrite ^/last/(.*) /test/$1 last;
    		return 403;
    }
    location /test/ {
    		return 508;
    }
    

    请求break:如果请求开头为/break/index.html 因为后面是break,不再进行重新匹配,就直接访问/test下的index.html文件内容,如果/test下没有文件就返回404找不到报错
    请求last:如果是请求开头为/last/index.html,因为后面是last,重新跳出本location进行匹配,/test/ 直接返回508(访问last相当于直接访问/test/)

  • 相关阅读:
    快速排序
    优先队列
    堆排序
    树、二叉树基础
    分治法
    递归算法详细分析
    算法基础
    Linux文件系统详解
    fs/ext2/inode.c相关函数注释
    块设备的读流程分析
  • 原文地址:https://www.cnblogs.com/zanao/p/12617628.html
Copyright © 2011-2022 走看看