zoukankan      html  css  js  c++  java
  • nginx url rewrite break和last的区别

     break 将重写的URI作为一个新的URI,在本块中继续处理,将重写后 的地址在当前location块中处理,不会将新的URI转向到其他location块中

    last,终止继续在本location块中处理接收到的URI,并将此处重写的URI作位一个新的URI,使用各location进行处理,该标志将重写后的URI重新在server块中执行,为重写后的URI提供转入到其他

    location的机会。

    Example 1: No (break or last) flags:

    server {
        server_name example.com;
        root 'path/to/somewhere';
    
        location / {
            echo 'finally matched location /';
        }
    
        location /notes {
            echo 'finally matched location /notes';
        }
    
        location /documents {
            echo 'finally matched location /documents';
        }
    
        rewrite ^/([^/]+.txt)$ /notes/$1;
        rewrite ^/notes/([^/]+.txt)$ /documents/$1;
    }

     Result:

    url example.com/test.txt
    finally matched location /documents

    Explanation:

    For rewrite, the flags are optional!

    Example 2: Outside location block (break or last):

    server {
        server_name example.com;
        root 'path/to/somewhere';
    
        location / {
            echo 'finally matched location /';
        }
    
        location /notes {
            echo 'finally matched location /notes';
        }
    
        location /documents {
            echo 'finally matched location /documents';
        }
    
        rewrite ^/([^/]+.txt)$ /notes/$1 break; # or last
        rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed
    }

    Result:

    #curl example.com/test.txt
    finally matched location /notes

    Explanation:

    Outside the location block, both break and last behave in the exact manner...

    no more parsing of rewrite conditions
    Nginx internal engine goes to the next phase (searching for location match)

    Example 3: Inside location block - "break":

    server {
        server_name example.com;
        root 'path/to/somewhere';
    
        location / {
            echo 'finally matched location /';
            rewrite ^/([^/]+.txt)$ /notes/$1 break;
            rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed
        }
    
        location /notes {
            echo 'finally matched location /notes';
        }
    
        location /documents {
            echo 'finally matched location /documents';
        }
    }

    Result:

    # curl example.com/test.txt
    finally matched location /

    Explanation:

    Inside a location block, break flag would do the following...

    no more parsing of rewrite conditions
    Nginx internal engine continues to parse the current location block

    Example 4: Inside location block - "last":

    server {
        server_name example.com;
        root 'path/to/somewhere';
    
        location / {
            echo 'finally matched location /';
            rewrite ^/([^/]+.txt)$ /notes/$1 last;
            rewrite ^/notes/([^/]+.txt)$ /documents/$1;  # this is not parsed
        }
    
        location /notes {
            echo 'finally matched location /notes';
            rewrite ^/notes/([^/]+.txt)$ /documents/$1;  # this is not parsed, either!
        }
    
        location /documents {
            echo 'finally matched location /documents';
        }
    }

    Result:

    # curl example.com/test.txt
    finally matched location /notes

    Explanation:

    Inside a location block, last flag would do the following...

    no more parsing of rewrite conditions
    Nginx internal engine starts to look for another location match based on the result of the rewrite result.
    no more parsing of rewrite conditions, even on the next location match!

    Summary:

    When a rewrite condition with the flag break or last matches, Nginx stops parsing any more rewrites!
    Outside a location block, with break or last, Nginx does the same job (stops processing anymore rewrite conditions).
    Inside a location block, with break, Nginx only stops processing anymore rewrite conditions
    Inside a location block, with last, Nginx stops processing anymore rewrite conditions and then starts to look for a new matching of location block! Nginx also ignores any rewrites in the new location block!

    Final Note:

    missed to include some more edge cases (actually common problem with rewrites, such as 500 internal error). But, that'd be out of scope of this question. Probably, example 1 is out of scope, too!

     

  • 相关阅读:
    mysql/mariadb学习记录——查询2
    mysql/mariadb学习记录——查询
    touchSwipe 上下左右滑动,二指缩放 效果不好。
    c# 调用c++ 使用指针传递的时候
    c# 调用 matlab 引发初始化错误 异常
    创建非主键唯一约束
    sql 中 如果添加 时间戳 字段 timestamp
    JS frame 跨域 传值
    VS 附加进程调试
    Aspose.words 替换字符 操作
  • 原文地址:https://www.cnblogs.com/Template/p/9082849.html
Copyright © 2011-2022 走看看