我不明白中断和最后(重写标志)之间的区别。 文档相当深奥。 我试图在我的一些configuration之间切换两个,但我不能发现任何行为上的差异。 有人可以更详细地解释这些标志吗? 优选地,在将一个标志翻转到另一个时示出不同的行为的示例。
您可能会针对不同位置有不同的重写规则。 当重写模块遇到last
,它停止处理当前设置,并且重写的请求再次被传递以find适当的位置(以及新的重写规则集合)。 如果规则以break
结束,则重写也会停止,但重写的请求不会传递到另一个位置。
也就是说,如果有两个位置:loc1和loc2,并且在loc1中有一个重写规则,它将loc1更改为loc2,并以last
结束,请求将被重写并传递到位置loc2。 如果规则以break
结束,则它将属于位置loc1。
OP首选一个例子。 另外,@minaev写道,只是故事的一部分! 那么,我们走吧…
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
,标志是可选的!
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出来。
location
匹配) 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
标志将执行以下操作…
location
块 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
标志将执行以下操作…
rewrite
结果的结果开始寻找另一个位置匹配。 break
或last
rewrite
条件匹配时,Nginx会停止parsing任何更多的rewrites
! break
,Nginx只会停止处理重写条件 last
,Nginx停止处理更多的重写条件,然后开始寻找一个新的location
块的匹配! Nginx也忽略了新的location
块的rewrites
。 我错过了包括更多的边缘情况(实际上是重写的常见问题,如500 internal error
)。 但是,这不在这个问题的范围之内。 例1也可能超出范围!