怎樣區別nginx中rewrite時break和last

怎樣區別nginxrewritebreaklast
在使用nginx配置rewrite中經常會遇到有的地方用last並不能工作,換成break就可以,其中的原理是對於根目錄的理解有所區別,按我的測試結果大致是這樣的。
  1. location /   
  2. {    
  3.     proxy_pass http://test;    
  4.     alias /home/html/;    
  5.     root /home/html;    
  6.     rewrite "^/a/(.*)\.html$" /1.html last;    
  7. }  
在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這個標籤上,再看看幾個測試結果:
  1. location /    
  2. {    
  3.     root /home/html;    
  4. }    
  5.   
  6. location /a/   
  7. {    
  8.     proxy_pass http://test;    
  9.     rewrite "^/a/(.*)\.html$" /1.html last;    
  10. }  
在這段配置裏,使用last訪問是可以訪問到東西的,不過,它出來的結果是:/home/html/1.html;可我需要的是http://test/1.html?使用break就可以了。
  1. location /   
  2. {    
  3.     root /home/html;   
  4. }    
  5.     
  6. location /a/   
  7. {    
  8.     proxy_pass http://test;    
  9.     rewrite "^/a/(.*)\.html$" /a/1.html last;   
  10. }  
在這段配置裏,返回錯誤,因爲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都不能成功。
轉自:http://user.qzone.qq.com/30879223/blog/1284178451
本文地址:http://www.92csz.com/19/912.html
如非註明則爲本站原創文章,歡迎轉載。轉載請註明轉載自:moon's blog
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章