nginx url重写:break和last的区别

我不明白中断和最后(重写标志)之间的区别。 文档相当深奥。 我试图在我的一些configuration之间切换两个,但我不能发现任何行为上的差异。 有人可以更详细地解释这些标志吗? 优选地,在将一个标志翻转到另一个时示出不同的行为的示例。

    您可能会针对不同位置有不同的重写规则。 当重写模块遇到last ,它停止处理当前设置,并且重写的请求再次被传递以find适当的位置(以及新的重写规则集合)。 如果规则以break结束,则重写也会停止,但重写的请求不会传递到另一个位置。

    也就是说,如果有两个位置:loc1和loc2,并且在loc1中有一个重写规则,它将loc1更改为loc2,并以last结束,请求将被重写并传递到位置loc2。 如果规则以break结束,则它将属于位置loc1。

    OP首选一个例子。 另外,@minaev写道,只是故事的一部分! 那么,我们走吧…

    示例1:否(中断或最后)标志

     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; } 

    结果:

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

    说明:

    为了rewrite ,标志是可选的!

    示例2:外部位置块(中断或最后)

     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 } 

    结果:

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

    说明:

    在位置区块之外,两者都以确切的方式performance出来。

    • 没有更多的重写条件的parsing
    • Nginx内部引擎进入下一个阶段(searchlocation匹配)

    示例3:内部位置块 – “中断”

     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'; } } 

    结果:

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

    说明:

    在一个位置块内, break标志将执行以下操作…

    • 没有更多的重写条件的parsing
    • Nginx内部引擎继续parsing当前的location

    示例4:内部位置块 – “最后”

     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'; } } 

    结果:

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

    说明:

    在一个位置块内, last标志将执行以下操作…

    • 没有更多的重写条件的parsing
    • Nginx内部引擎根据rewrite结果的结果开始寻找另一个位置匹配。
    • 没有更多的重写条件parsing,即使在下一个位置匹配!

    概要:

    • 当标志breaklast rewrite条件匹配时,Nginx会停止parsing任何更多的rewrites
    • 在一个位置块之外,Nginx做了同样的工作(停止处理重写条件)。
    • 在一个位置块内,如果有break ,Nginx只会停止处理重写条件
    • 在一个位置块内, last ,Nginx停止处理更多的重写条件,然后开始寻找一个新的location块的匹配! Nginx也忽略了新的location块的rewrites

    最后注意:

    我错过了包括更多的边缘情况(实际上是重写的常见问题,如500 internal error )。 但是,这不在这个问题的范围之内。 例1也可能超出范围!