怎样区别nginx中rewrite时break和last

在使用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

猜你喜欢

转载自hugoren.iteye.com/blog/2215106
今日推荐