nginx 重写中断标志如何工作?

nginx 重写中断标志如何工作?

我正在尝试在网站上设置“即将推出”的临时页面:

rewrite ^/(css|img|js)/ -                break;
rewrite ^/$             /comingsoon.html last;
rewrite ^               /?               redirect;

但是我在使用该标志时遇到了麻烦break:它应该停止处理重写规则,但它似乎不起作用。

我的目的是:

  1. 按原样提供所有 css、img 和 js 文件;
  2. 使用 comingsoon.html 页面代替主页;
  3. (临时)将所有其他页面重定向至主页。

问题在于指令 1. 不起作用:它的 URL 会遵循第 3 条规则并被重定向到主页,就好像第一条规则没有标志一样break

第一个指令中的正则表达式确实匹配,因为如果我用 替换breakredirect我会得到 302 错误-。因此,break标志就是罪魁祸首。

是不是我用得不正确?


编辑:我用负面前瞻解决了这个问题:

rewrite ^/$                 /comingsoon.html last;
rewrite ^/(?!css/|img/|js/) /?               redirect;

但我仍然有兴趣了解为什么break它似乎不起作用。

答案1

刚刚打开重写日志和调试日志,发现你的问题在于重写为-。与 Apache 不同,nginx 按字面意思理解,将请求 URI 重写为字符串-。然后它找不到location与此 URI 匹配的任何块,并使用它的魔法 configuration ""刚好包含所有服务器rewrite规则。因此,新 URI 会再次通过所有重写规则。

以下是日志:

2016/06/01 19:52:41 [debug] 7288#7288: *24 http header: "Host: example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http header: "User-Agent: curl/7.47.0"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http header: "Accept: */*"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http header done
2016/06/01 19:52:41 [debug] 7288#7288: *24 event timer del: 4: 1464800021049
2016/06/01 19:52:41 [debug] 7288#7288: *24 generic phase: 0
2016/06/01 19:52:41 [debug] 7288#7288: *24 rewrite phase: 1
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex: "^/css/"
2016/06/01 19:52:41 [notice] 7288#7288: *24 "^/css/" matches "/css/asd", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script copy: "-"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex end
2016/06/01 19:52:41 [notice] 7288#7288: *24 rewritten data: "-", args: "", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 test location: "/"
2016/06/01 19:52:41 [debug] 7288#7288: *24 using configuration ""
2016/06/01 19:52:41 [debug] 7288#7288: *24 http cl:-1 max:1048576
2016/06/01 19:52:41 [debug] 7288#7288: *24 rewrite phase: 3
2016/06/01 19:52:41 [debug] 7288#7288: *24 rewrite phase: 4
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex: "^/css/"
2016/06/01 19:52:41 [notice] 7288#7288: *24 "^/css/" does not match "-", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex: "^/$"
2016/06/01 19:52:41 [notice] 7288#7288: *24 "^/$" does not match "-", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex: "^"
2016/06/01 19:52:41 [notice] 7288#7288: *24 "^" matches "-", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script copy: "/"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex end
2016/06/01 19:52:41 [notice] 7288#7288: *24 rewritten redirect: "/", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"

解决初始问题可能很简单:

rewrite ^(/(css|img|js)/.+) $1 break;

或者

rewrite ^/(css|img|js)/ $uri break;

那么你应该注意你重写了你的 URI。

相关内容