【问题标题】:Silly nginx rewrite issue - infinate loop愚蠢的 nginx 重写问题 - 无限循环
【发布时间】:2016-04-28 18:07:06
【问题描述】:

我对这个完全感到困惑。它可能很愚蠢,一天后我很想念它!无论如何,我在网站的 nginx 配置中设置了这个重写规则:

location / {
    root   /srv/www/site.co.uk/www;
    index  index.html index.htm index.php;

    rewrite ^/(.*)/index.html$ http://site.co.uk/$1/ permanent;
    rewrite ^/index.html$ http://site.co.uk/ permanent;
}

当我去:

..然后它正确发送到:

如果我将这 2 个重写规则注释掉,重启 nginx,然后重试...页面加载正常!

谁能看出我哪里出错了?也许我只是瞎了眼!

【问题讨论】:

  • 不清楚您遇到的问题是什么?你什么时候看到无限循环?

标签: nginx url-rewriting


【解决方案1】:

你已经构建了一个重写循环。

每当出现带有尾随 / 的 URL 时,index 指令有效地生成对 /index.html 的内部重写。

打破循环的一种方法是仅在外部 URL 包含 index.html 时应用您的 rewrite 规则。变量 $request_uri 包含外部 URL,可以使用 if 指令进行测试。关于if,请参阅this caution

if ($request_uri ~* "/index\.html(?|$)") {
    rewrite ^(.*/)index\.html$ $scheme://$server_name$1 permanent;
}
location / {
    root   /srv/www/site.co.uk/www;
    index  index.html index.htm index.php;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2011-06-09
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多