简体   繁体   English

Nginx中的Last&Break机制重写

[英]The last & break mechanism in nginx rewrite

Am now dealing with nginx rewrite for my project but got something out of my expectation, just share with you to see if any reasonable advice on this. 我现在正在为我的项目处理nginx重写,但出乎我的意料,请与您共享以了解是否有合理的建议。

The nginx server setting list below: 下面的Nginx服务器设置列表:

location /download/ {
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3;
    rewrite ^(/download/.*)/movie/(.*)\..*$ $1/avi/$2.mp3 break;
    rewrite ^(/download/.*)/avvvv/(.*)\..*$ $1/rmvb/$2.mp3;
}

From above section, you can see that I only add one download location with three rewrite rules.I open the debug log in nginx and start it to load this setting. 从上面的部分可以看到,我仅添加了一个具有三个重写规则的下载位置。我在nginx中打开调试日志并启动它以加载此设置。

Now we enter url: localhost/download/123/movie/UBW.avi in our browser. 现在,在浏览器中输入url:localhost / download / 123 / movie / UBW.avi。 From the log, we can see that the rules are hit, logs below: 从日志中,我们可以看到符合规则,日志如下:

2017/05/22 15:27:27 [notice] 1904#12520: *85 "^(/download/.*)/media/(.*)\..*$" does not match "/download/123/movie/UBW.avi", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
2017/05/22 15:27:27 [notice] 1904#12520: *85 "^(/download/.*)/movie/(.*)\..*$" matches "/download/123/movie/UBW.avi", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
2017/05/22 15:27:27 [notice] 1904#12520: *85 rewritten data: "/download/123/avi/UBW.mp3", args: "", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
2017/05/22 15:27:27 [error] 1904#12520: *85 CreateFile() "D:\Soft\nginx-1.13.0/html/download/123/avi/UBW.mp3" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"

From above info, we can see that the second rule is hit and the section break due to the "break" keyword. 从上面的信息中,我们可以看到第二条规则已命中,并且由于“ break”关键字而导致了部分中断。 This behavior is in our expectation. 这种行为是我们的预期。

But wen I changed the section like below and behave the same behavior as above: 但是我改变了下面的部分,并表现出与上面相同的行为:

location /download/ {
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3;
    rewrite ^(/download/.*)/movie/(.*)\..*$ $1/avi/$2.mp3 last;
    rewrite ^(/download/.*)/avvvv/(.*)\..*$ $1/rmvb/$2.mp3;
}

The log below: 下面的日志:

2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/media/(.*)\..*$" does not match "/download/123/movie/UBW.avi", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/movie/(.*)\..*$" matches "/download/123/movie/UBW.avi", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
2017/05/22 15:33:45 [notice] 11244#11544: *92 rewritten data: "/download/123/avi/UBW.mp3", args: "", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/media/(.*)\..*$" does not match "/download/123/avi/UBW.mp3", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/movie/(.*)\..*$" does not match "/download/123/avi/UBW.mp3", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/avvvv/(.*)\..*$" does not match "/download/123/avi/UBW.mp3", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
2017/05/22 15:33:45 [error] 11244#11544: *92 CreateFile() "D:\Soft\nginx-1.13.0/html/download/123/avi/UBW.mp3" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"

From the second row of the log, we can see that the rule is hit, but then nginx start a new loop for the download section, the strange thing is that , it didn't hit the rule, why? 从日志的第二行,我们可以看到规则已被执行,但是随后nginx为下载部分启动了一个新循环,奇怪的是,它没有达到规则,为什么?

Also, if we use last keyword, then how many times will the nginx try to loop the section, in default it's two, right? 另外,如果我们使用last关键字,那么nginx将尝试循环多少次,默认情况下为两次,对吗?

The ngx_http_rewrite_module module is used to change request URI using PCRE regular expressions ngx_http_rewrite_module模块用于使用PCRE正则表达式更改请求URI

Remember that when you rewrite the uri, the uri will change . 请记住,当您重写uri时,uri将会改变。

For last last

When rewrite module meets last, it stops processing the current set and the rewritten request is passed once again to find the appropriate location (and the new set of rewriting rules). 当重写模块最后一次遇到时,它将停止处理当前集,并且再次传递重写的请求以查找适当的位置(以及新的重写规则集)。

In your configure 在您的配置中

location /download/ {
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3;  //a
    rewrite ^(/download/.*)/movie/(.*)\..*$ $1/avi/$2.mp3 last; //b
    rewrite ^(/download/.*)/avvvv/(.*)\..*$ $1/rmvb/$2.mp3;    //c
}

your url localhost/download/123/movie/UBW.avi 您的网址localhost/download/123/movie/UBW.avi

your request uri is /download/123/movie/UBW.avi 您的要求uri是/download/123/movie/UBW.avi


first pass 首过

the /download/123/movie/UBW.avi match b ,so the uri rewrite to /download/123/avi/UBW.mp3 ,the first pass finish. /download/123/movie/UBW.avi匹配b ,因此uri将重写为/download/123/avi/UBW.mp3 ,第一遍完成。


second pass 第二遍

And then, in the second pass ,the uri is 然后,在第二遍中,uri是

/download/123/avi/UBW.mp3

So it do not match abc , finally , it throw error in log. 因此,它与abc不匹配,最后,它在日志中引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM