【问题标题】:NGINX return 301 instead of 404NGINX 返回 301 而不是 404
【发布时间】:2020-06-07 01:49:49
【问题描述】:

我在 Centos 7 上使用 NGINX。 我阻止了所有 PHP 请求,因为我的 Web 服务器只有静态 html 文件。 我的 NGINX 配置如下;

server {
listen myIp:80;
server_name myDomain;

location ~(\.php$) {
    return 403;
}

return 301 https://myDomain$request_uri;

}

但它返回 301 请求 index.php。我的 access.log 如下所示;

43.226.148.141 - - [23/Feb/2020:04:36:54 +0900] "GET /mysql/admin/index.php HTTP/1.1" 301 162 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"

我预计 NGINX 将返回 404,因为 index.php 不存在或返回 403,因为我添加了限制,但在 access.log 中它返回 301。 当我通过在网络浏览器上输入“myDomain/mysql/admin/index.php”来尝试它时,我在 301 之后得到 403。

谁能解释一下我的 NGINX 服务器上发生了什么并帮我解决这个问题?

【问题讨论】:

    标签: nginx configuration centos


    【解决方案1】:

    location ~(\.php$) { 不匹配,因为它在~ 和正则表达式本身之间缺少一个空格。此外,无需创建捕获组。因此,更好:

    location ~ \.php$ {
        return 403;
    }
    

    【讨论】:

      【解决方案2】:

      即使位置匹配,也将始终执行返回 301,因为它不在特定位置的范围内。尝试重写 Nginx 配置,如下所示

      location ~ \.php$ {
        return 403;
      }
      
      location / {
        return 301 https://myDomain$request_uri;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-04
        • 2022-06-23
        • 2020-07-30
        • 2018-06-28
        • 2011-05-04
        相关资源
        最近更新 更多