Nginx 关于 Rewrite 执行顺序详解第一篇: break 和 last 的区别REFER: http://wiki.nginx.org/NginxHttpRewriteModule#rewriteIf the directives of this module are given at the server level, then they are carried out before the location of the request is determined. If in that selected location there are further rewrite directives, then they also are carried out. If the URI changed as a result of the execution of directives inside location, then location is again determined for the new URI. This cycle can be repeated up to 10 times, after which Nginx returns a 500 error. Rewrite( URL 重写)指令可以出现在 server{} 下,也可以出现在 location{} 下,它们之间是有区别的!对于出现在 server{} 下的 rewrite 指令,它的执行会在 location 匹配之前;对于出现在 location{} 下的 rewrite 指令,它的执行当然是在 location 匹配之后,但是由于 rewrite 导致 HTTP 请求的 URI 发生了变化,所以 location{} 下的 rewrite后的 URI 又需要重新匹配 location ,就好比一个新的 HTTP 请求一样(注意由 location{} 内的 rewrite 导致的这样的循环匹配最多不超过 10 次,否则 nginx 会报 500 错误)。总的来说,如果 server{} 和 location{} 下都有 rewrite,依然是先执行 server{} ,然后进行 location 匹配,如果被匹配的 location{} 之内还有 rewrite 指令,那么继续执行 rewrite ,同时因为 location{} 内的 rewrite 改变了 URI ,那么重写后的结果 URI 需要当做一个新的请求,重新匹配 location (应该也包括重新执行 server{} 下的 rewrite 吧)。 Last 与 break flag 的区别关于 last flag 和 break flag 的区别,官方文档的描述是:“ last - completes processing of rewrite directives, after which searches for corresponding URI and location ”和“ break - completes processing of rewrite directives ”,都有“不让继续执行后面的 rewrite 指令”的含义,但是两者的区别并没有展开。这里我用实验来告诉大家区别。实验准备: 1、 安装 nginx ;(如果对安装和 location 不了解的,请参考: http://eyesmore.iteye.com/blog/1141660 ) 2、 在 nginx 安装目录的 html 子目录下创建 4 个文件,分别叫: aaa.html , bbb.html , ccc.html 和 ddd.html,文件内容分别是各自的文件名(例 aaa.html 文件内容不妨写 aaa html file )。 3、 Nginx 配置文件初始化是: error_log logs/error.log info; #URL 重写模块的日志会写入此文件 server { listen 9090; server_name localhost; root html; rewrite_log on; # 打开 URL 重写模块的日志开关,以便写入 error_log location /aaa.html { rewrite "^/aaa\.html$" /bbb.html; rewrite "^/bbb\.html$" /ddd.html; } location /bbb.html { rewrite "^/bbb\.html$" /ccc.html; } } 上述配置注意两点: 1 、打开 rewrite 模块的日志开关,以便 rewrite 执行日志写入 error_log (注: rewrite 日志写入 error_log 的级别是 notice ,所以要注意 error_log 日志级别,此处用 info ); 2 、定义了两个 location ,分别是 /aaa.html 和 /bbb.html ,但是在 /aaa.html 中,把 /aaa.html 重写成 /bbb.html ,接着又把 /bbb.html 重写成/ddd.html ;在 /bbb.html 中,把 /bbb.html 重写成 /ccc.html 。 [ 测试 1] 没有 last 和 break 标记时:请求 aaa.html [root@web108 ~]# curl http://localhost:9090/aaa.html ddd html file [root@web108 ~]# Error_log 的日志内容: 2011/08/07 22:13:23 [notice] 9066#0: *85 "^/aaa\.html$" matches "/aaa.html" , client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:13:23 [notice] 9066#0: *85 rewritten data: "/bbb.html" , args: "", client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:13:23 [notice] 9066#0: *85 "^/bbb\.html$" matches "/bbb.html" , client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:13:23 [notice] 9066#0: *85 rewritten data: "/ddd.html" , args: "", client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:13:23 [info] 9066#0: *85 client 127.0.0.1 closed keepalive connection URL 重写模块的日志告诉我们:对于一个 HTTP 请求“ GET /aaa.html ”,重写过程是:先 /aaa.html 被重写为/bbb.html ;然后 rewritten data: /bbb.html ,继续执行后面的 rewrite 指令,进而被重写为 /ddd.html ,然后rewrittern data: /ddd.html 后面没有重写了(其实此时 /ddd.html 需要再次重新匹配 location 的,只是日志没有体现出来,接下来的测试 2 会体现这点),于是输出 /ddd.html 的内容。 [ 测试 2] 使用 last 标记时:请求 aaa.html 将上述 location /aaa.html {} 修改成: location /aaa.html { rewrite "^/aaa\.html$" /bbb.html last ; rewrite "^/bbb\.html$" /ddd.html; } 测试结果: [root@web108 ~]# curl http://localhost:9090/aaa.html ccc html file [root@web108 ~]# Error_log 日志: 2011/08/07 22:24:31 [notice] 18569#0: *86 "^/aaa\.html$" matches "/aaa.html" , client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:24:31 [notice] 18569#0: *86 rewritten data: "/bbb.html" , args: "", client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:24:31 [notice] 18569#0: *86 "^/bbb\.html$" matches "/bbb.html" , client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:24:31 [notice] 18569#0: *86 rewritten data: "/ccc.html" , args: "", client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:24:31 [info] 18569#0: *86 client 127.0.0.1 closed keepalive connection 不知道读者看到 GET /aaa.html 显示的结果“ ccc html file ”会不会惊讶:“为什么结果不是 bbb html file ”。下面解释下整个过程:首先 /aaa.html 匹配了 location /aaa.html {} ,于是执行 rewrite "^/aaa\.html$" /bbb.html last ,把/aaa.html 重写为 /bbb.html ,同时由于 last flag 的使用,后面的 rewrite 指令(指的是 rewrite "^/bbb\.html$" /ddd.html )不会被执行。似乎此时应该输出“ bbb html file ”才对,但是我们看看 nginx 官方解释:“ last - completes processing of rewrite directives, after which searches for corresponding URI and location ”意思是说last 不再匹配后面的 rewrite 指令,但是紧接着需要对重写后的 URI 重新匹配 location 。让我们再看看官方的“ If the directives of this module are given at the server level, then they are carried out before the location of the request is determined. If in that selected location there are further rewrite directives, then they also are carried out. If the URI changed as a result of the execution of directives inside location, then location is again determined for the new URI. This cycle can be repeated up to 10 times, after which Nginx returns a 500 error.”因此,重新匹配的时候,匹配到了新的 location /bbb.html {} ,执行“ rewrite "^/bbb\.html$" /ccc.html ”,最后的内容是“ ccc html file ”。 [ 测试 3] 使用 break 标记时:请求 aaa.html 将上述 location /aaa.html {} 修改成使用 break 标记: location /aaa.html { rewrite "^/aaa\.html$" /bbb.html break ; rewrite "^/bbb\.html$" /ddd.html; } 测试结果: [root@web108 ~]# curl http://localhost:9090/aaa.html bbb html file [root@web108 ~]# 日志结果: 2011/08/07 22:37:49 [notice] 21069#0: *89 "^/aaa\.html$" matches "/aaa.html" , client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:37:49 [notice] 21069#0: *89 rewritten data: "/bbb.html" , args: "", client: 127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1", host: "localhost:9090" 2011/08/07 22:37:49 [info] 21069#0: *89 client 127.0.0.1 closed keepalive connection 我想这个结果不用多做解释了,充分体现了 break 和 last 的区别:“ last - completes processing of rewrite directives, after which searches for corresponding URI and location ”和“ break - completes processing of rewrite directives ”。 Break 和 last 都能阻止继续执行后面的 rewrite 指令,但是 last 如果在 location 下用的话,对于重写后的 URI 会重新匹配 location ,但是 break 则不会重新匹配 location 。简单的说, break 终止的力度比 last 更加彻底(为了记忆的方便,我们可以把重新后的 URI 重新匹配 location 理解为“ URI 匹配 location 的循环语句的下一次迭代”,高级程序设计里面 break 一般用做退出循环,所以 break 不仅终止继续执行 rewrite ,而且退出 URI 重新匹配 location 的循环迭代)。 (责任编辑:IT) |