【问题标题】:nginx rewrite with try_files 403nginx 用 try_files 403 重写
【发布时间】:2013-12-22 17:52:34
【问题描述】:

我有一个带有 /usercp/ 和 usercp.php 的网络服务器。我正在使用 tryfiles 并重新编写以查看 file.php 是否存在做文件,否则转到 /file/ (在我的情况下 file = usercp)

这是我的 nginx 配置文件。

location / {
    try_files $uri $uri/ @extension-php;
}

location @extension-php {
    rewrite ^(.*)$ $1.php last;
}

这也会使 site.com/usercp/ 出现 403 错误。有什么想法吗?

【问题讨论】:

  • 是的,但是 /usercp/.php 不存在。您应该考虑尾部斜杠的可能性。试试这个rewrite ^(.*)/?$ $1.php last;

标签: nginx


【解决方案1】:

问题是,如果你想要相反的情况,你的文件夹索引优先于 php 文件,我建议不要使用 autoindex on,因为它会暴露文件夹的内容并交换 try_files 中的最后两项,试试这个

location / {
    try_files $uri $uri.php $uri/;
}

PS: 如果$uri/ 不包含index 中指定的索引文件,它将始终返回403,因为默认情况下它禁止列出文件夹,如果是,您应该放置索引文件您打算做什么,或者只是从try_files 中删除整个$uri/,以便它返回404 而不是403

【讨论】:

    【解决方案2】:

    http://nginx.org/r/try_files

    它所做的只是检查文件是否存在,然后提供存在的文件。

    您声称/usercp/ 存在。因此,这就是它将尝试提供的服务。但是您可能没有 autoindex on,因此不允许列出目录 — 403 Forbidden

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 2010-11-25
      • 2012-04-24
      • 1970-01-01
      相关资源
      最近更新 更多