【问题标题】:"No input file specified." error encountered instead of 404 error in NGINX“没有指定输入文件。”在 NGINX 中遇到错误而不是 404 错误
【发布时间】:2017-03-23 04:49:22
【问题描述】:

我面临着同样可怕的“未指定输入文件”。 NGINX 中的消息。下面是我想要实现的目标的简要说明。

  1. 任何以“.php”扩展名结尾的现有 URL 都应显示为 404 错误 [工作中]
  2. 任何没有“.php”扩展名的现有 URL 都将执行文件 [工作]
  3. 任何不存在的 URL 都应该抛出 404 错误 [不工作]。在这种情况下,NGINX 抛出的错误是“没有指定输入文件”。

我已访问并尝试实施位于 Nginx + php fastcgi showing "No input file specified." instead of 404 的解决方案,但这并没有解决我的问题。

我真的不确定如何在第 3 点的情况下呈现 404 页面。

/etc/nginx/sites-enabled/default 文件的内容如下

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html/example.com;
    index index.php index.html index.htm;
    server_name example.com;
    error_page 404 /error/custom_404;

    location / {
        try_files $uri $uri/  @missing;
    }

    location ~ (\.php$|myadmin) {
    return 404;
    }

    location @missing {
            fastcgi_param SCRIPT_FILENAME "$document_root$uri.php";
            fastcgi_param PATH_TRANSLATED "$document_root$uri.php";
            fastcgi_param QUERY_STRING    $args;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

请记住,我只是一个初学者而不是专家。任何帮助表示赞赏。

【问题讨论】:

    标签: php nginx


    【解决方案1】:

    在将文件传递给php-fpm 之前,您的location @missing 块不会验证文件是否存在。添加文件存在检查:

    location @missing {
        if (!-f $document_root$uri.php) { return 404; }
        ...
    }
    

    请参阅this document 了解更多信息,以及this caution 了解if 的使用。

    【讨论】:

    • 在缺失的块中添加 try_files $uri.php =404; 会为之前服务的所有请求引发 404 错误。 try_files 存在于它上面的另一个块中。
    • 对不起,try_files 更改了 URI。答案更新为使用if 语句。
    • 非常感谢@RichardSmith。这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多