【问题标题】:nginx - how do I get rewrite directives to execute before index directives?nginx - 如何在索引指令之前执行重写指令?
【发布时间】:2010-04-12 17:22:15
【问题描述】:

我正在尝试使用 nginx 进行简单的内部重写,以根据 user_agent 导航到子目录——移动浏览器转到 /mobile,否则它们转到 /www

然而,当我重写这些 url 时,似乎在重写之前处理了 index 指令,所以我最终得到了 403 禁止。

# TEST FOR INDEX
index index.php

# TEST PHONES
if ($http_user_agent ~* '(iPhone|iPod)') {
    rewrite ^(.*)$ /mobile$1 break;
}

# OTHERWISE WE ARE DONE
rewrite ^(.*)$ /www$1 break;

当我关闭重写并点击主机名 (http://www.somehost.com/) 时,索引会正确显示。当它们打开时,我必须显式导航到 somehost.com/index.php 以使脚本运行...

我是否必须明确测试目录,然后重新写入 index.php 文件,还是有更简单的解决方案?

【问题讨论】:

    标签: url-rewriting nginx


    【解决方案1】:

    尝试:

    server {
      index index.php;
      location / {
        if ($http_user_agent ~* '(iPhone|iPod)') {
          rewrite ^(.*)$ /mobile$1 last;
        }
        rewrite ^(.*)$ /www$1 last;
      }
    }
    

    【讨论】:

      【解决方案2】:

      这是双重调用的问题。哎呀。应该知道的。

      第一个请求以 / 的形式出现,然后重新写入 /www/。

      然后应用了索引,所以它变成了/www/index.php,但是php处理程序正在重新调用重写规则,所以最终的url变成了:/www/www/index.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-14
        • 2013-05-30
        • 2011-08-23
        • 1970-01-01
        • 2011-12-12
        • 1970-01-01
        • 1970-01-01
        • 2021-05-14
        相关资源
        最近更新 更多