在使用nginx配置rewrite中經常會遇到有的地方用last并不能工作,換成break就可以,其中的原理是對于根目錄的理解有所區別,按我的測試結果大致是這樣的。
location / { proxy_pass http://test; alias /home/html/; root /home/html; rewrite "^/a/(.*)/.html$" /1.html last; }
在location / { 配置里:
1、使用root指定源:使用last和break都可以
2、使用proxy_pass指定源:使用last和break都可以
3、使用alias指定源:必須使用last
在location /a/或使用正則的location ~ ^/a/里:
1、使用root指定源:使用last和break都可以
2、使用proxy_pass指定源:使用break和last結果有所區別
3、使用alias指定源:必須使用last
其中區別主要在proxy_pass這個標簽上,再看看幾個測試結果:
location / { root /home/html; } location /a/ { proxy_pass http://test; rewrite "^/a/(.*)/.html$" /1.html last; }
在這段配置里,使用last訪問是可以訪問到東西的,不過,它出來的結果是:/home/html/1.html;可我需要的是http://test/1.html?使用break就可以了。
location / { root /home/html; } location /a/ { proxy_pass http://test; rewrite "^/a/(.*)/.html$" /a/1.html last; }
在這段配置里,返回錯誤,因為last會重新發起請求匹配,所以造成了一個死循環,使用break就可以訪問到http://test/a/1.html。
所以,使用last會對server標簽重新發起請求,而break就直接使用當前的location中的數據源來訪問,要視情況加以使用。一般在非根的location中配置rewrite,都是用的break;而根的location使用last比較好,因為如果配置了fastcgi或代理訪問jsp文件的話,在根location下用break是訪問不到。測試到rewrite有問題的時候,也不妨把這兩者換換試試。
至于使用alias時為什么必須用last,估計是nginx本身就限定了的,怎么嘗試break都不能成功。
所以我們再來理解last與break的區別:
last: 停止當前這個請求,并根據rewrite匹配的規則重新發起一個請求。新請求又從第一階段開始執行…
break:相對last,break并不會重新發起一個請求,只是跳過當前的rewrite階段,并執行本請求后續的執行階段…
我們再來看一個例子:
server { listen 80 default_server; server_name dcshi.com; root www; location /break/ { rewrite ^/break/(.*) /test/$1 break; echo "break page"; } location /last/ { rewrite ^/last/(.*) /test/$1 last; echo "last page"; } location /test/ { echo "test page"; }}
請求:http://dcshi.com/break/***
輸出: break page
分析:正如上面討論所說,break是跳過當前請求的rewrite階段,并繼續執行本請求的其他階段,很明顯,對于/foo 對應的content階段的輸出為 echo “break page”; (content階段,可以簡單理解為產生數據輸出的階段,如返回靜態頁面內容也是在content階段;echo指令也是運行在content階段,一般情況下content階段只能對應一個輸出指令,如同一個location配置兩個echo,最終只會有一個echo指令被執行);當然如果你把/break/里的echo 指令注釋,然后再次訪問/break/xx會報404,這也跟我們預期一樣:雖然/break/xx被重定向到/test/xx,但是break指令不會重新開啟一個新的請求繼續匹配,所以nginx是不會匹配到下面的/test/這個location;在echo指令被注釋的情況下,/break/ 這location里只能執行nginx默認的content指令,即嘗試找/test/xx這個html頁面并輸出起內容,事實上,這個頁面不存在,所以會報404的錯誤。