在使用nginx配置rewrite中經常會遇到有的地方用last並不能工作,換成break就可以,其中的原理是對於根目錄的理解有所區別,按我的測試結果大致是這樣的。
- location /
- {
- proxy_pass http://test;
- alias /home/html/;
- root /home/html;
- rewrite "^/a/(.*)\.html$" /1.html last;
- }
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;
- }
- location /
- {
- root /home/html;
- }
- location /a/
- {
- proxy_pass http://test;
- rewrite "^/a/(.*)\.html$" /a/1.html last;
- }
所以,使用last會對server標籤重新發起請求,而break就直接使用當前的location中的數據源來訪問,要視情況加以使用。一般在非根的location中配置rewrite,都是用的break;而根的location使用last比較好,因爲如果配置了fastcgi或代理訪問jsp文件的話,在根location下用break是訪問不到。測試到rewrite有問題的時候,也不妨把這兩者換換試試。
至於使用alias時爲什麼必須用last,估計是nginx本身就限定了的,怎麼嘗試break都不能成功。
轉自:http://user.qzone.qq.com/30879223/blog/1284178451
如非註明則爲本站原創文章,歡迎轉載。轉載請註明轉載自:moon's blog