last,終止繼續在本location塊中處理接收到的URI,並將此處重寫的URI做位一個新的URI,使用各location進行處理,該標誌將重寫後的URI從新在server塊中執行,爲重寫後的URI提供轉入到其餘curl
location的機會。ide
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; }
url example.com/test.txt finally matched location /documents
For rewrite, the flags are optional!this
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
Outside the location block, both break and last behave in the exact manner...url
no more parsing of rewrite conditions
Nginx internal engine goes to the next phase (searching for location match)spa
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 /
Inside a location block, break flag would do the following...code
no more parsing of rewrite conditions
Nginx internal engine continues to parse the current location blockserver
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
Inside a location block, last flag would do the following...blog
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!it
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!io
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!