使用php Fast/CGI时出现“未指定输入文件”的错误

27

我试图为Nginx设置配置,并遇到了一些问题。

在我的sites-available目录中有一个名为default的文件,其中包含以下代码:

server {
    server_name www.test.com test.com;
    access_log /sites/test/logs/access.log;
    error_log /sites/test/logs/error.log;
    root /sites/test;

 location ~ / {
    
index index.php
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

当我写URL时,上述代码运行得非常完美。

www.test.com/service/public/
当我输入 www.test.com/service/public/testservice(testservice是public文件夹中的一个文件夹)时,它显示No input file specified.。如何解决这个问题?我尝试了下面的方法,但没有成功。
http://nginxlibrary.com/resolving-no-input-file-specified-error/
http://blog.martinfjordvald.com/2011/01/no-input-file-specified-with-php-and-nginx/

不确定是否有帮助,但我遇到了同样的问题,在使用“chmod”更改站点目录权限后,它又可以正常工作了。 - Craig Wayne
你把它chmod成什么了? - Khom Nazid
这个回答解决了你的问题吗?未指定输入文件 - Paulo Boaventura
20个回答

35

你必须在其中添加"include fastcgi.conf"

location ~ \.$php{
  #......
  include fastcgi.conf;
}

3
谢谢!我也遇到了同样的问题(在将nginx 1.2升级到1.6后),我进行了更改:include fastcgi_params; 改为:include fastcgi.conf; 然后就可以工作了!干杯! - mzalazar
我尝试过了,得到了相同的结果。这是我的配置:https://jsfiddle.net/bheng/t6jepya5/。任何提示将不胜感激! - code-8
你的 $ 符号放错了位置 - 应该是 location ~ \.php$ { - Mike

23

解决“未指定输入文件”错误

如果您正在使用带有php-cgi的nginx,并且已经按照标准程序进行设置,那么您可能经常会遇到“未指定输入文件”错误。该错误通常是由于php-cgi守护程序无法使用提供给它的SCRIPT_FILENAME参数找到要执行的.php文件而引起的。我将讨论该错误的常见原因和解决方案。 发送到php-cgi守护进程的路径错误

往往情况下,错误的路径(SCRIPT_FILENAME)被发送到了fastCGI守护进程。在许多情况下,这是由于错误配置造成的。我看到过一些这样的设置:

server {
    listen   [::]:80;
    server_name  example.com www.example.com;
    access_log  /var/www/logs/example.com.access.log;  

    location / {
        root   /var/www/example.com;
        index  index.html index.htm index.pl;
    }

    location /images {
        autoindex on;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;
        include fastcgi_params;
    }
}

现在,这个配置存在许多问题。一个明显且突出的问题是在位置 / 块中定义的根指令。当根在位置块内定义时,它仅适用于该块。在这里,位置 /images 块将不匹配任何请求,因为它没有任何 $document_root 定义,我们必须为其重新定义根路径。显然,根指令应该移出位置 / 块并在服务器块中定义。这样,位置块将继承父级服务器块中定义的值。当然,如果您想为位置定义不同的 $document_root,则可以在位置块中放置根指令。

另一个问题是快速CGI参数 SCRIPT_FILENAME 的值是硬编码的。如果我们更改根指令的值并将文件移动到目录链的其他位置,php-cgi 将返回“未指定输入文件”错误,因为无法在硬编码的位置找到文件,该位置在更改 $document_root 时未更改。因此,我们应该将SCRIPT_FILENAME设置如下:

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
我们应该记住,根指令应该在服务器块中,否则只会将$fastcgi_script_name作为SCRIPT_FILENAME传递,并且我们会收到“未指定输入文件”错误提示。 来源(解决“未指定输入文件”错误)

在文件 /etc/php-fpm.d/www.conf 中更改用户:nginx组:nginx - evergreen

12

简单地重新启动我的php-fpm解决了这个问题。根据我的理解,这主要是一个php-fpm问题而不是nginx。


4

有同样的问题。

原因:我的网站根目录没有在open_basedir中指定。
解决方案:将我的网站根目录添加到:

/etc/php5/fpm/pool.d/mysite.conf<br>

通过添加这个指令:
php_value[open_basedir] = /my/root/site/dir:/other/directory/allowed

3
我尝试了上面的所有设置,但这个解决了我的问题。您需要定义nginx来检查该位置实际上是否存在php文件。我发现 try_files $uri = 404; 解决了这个问题。
location ~ \.php$ {
        try_files  $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock; 
        include fastcgi_params;
        fastcgi_index index.php;
}

这与现有的答案有何不同? - Stephen Rauch
try_files $uri = 404; - Rudra Pratap Sinha
1
你应该将那些信息放入你的答案中。如果它能解释为什么它与其他答案不同/更好,那么这个答案会更有帮助。 - Stephen Rauch
这个解决方案实际上用标准的nginx 404响应替换了php-fpm的“未指定输入文件”的响应。这正是我一直在寻找的! - Vladimir

3

我通过替换解决了它。

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

$document_root为C:\MyWebSite\www\所指向的目录。

fastcgi_param SCRIPT_FILENAME C:\MyWebSite\www\$fastcgi_script_name;

3

这些答案没有帮助我,我的php管理器仍然显示"未指定输入文件"错误。

但我知道我之前改变了php版本。

所以,我找到了原因:不是nginx,而是php.ini文档根参数!

我在php.ini中找到了

doc_root =

并将其更改为

;doc_root =

使用此补丁后,我的管理器工作正常。


2
这可能是因为在URL末尾加上斜杠时,NGinx会尝试查找默认索引文件,通常是index.html,如果没有配置的话。在没有斜杠的情况下,它试图匹配无法找到的文件testservice。或者你的testservice文件夹中没有默认索引文件。尝试将以下行添加到你的服务器配置中:
index  index.php index.html index.htm; // Or in the correct priority order for you

希望这可以帮到你!
编辑
我的回答不是很清晰,看这个例子就能明白我想说什么。
listen 80;

    server_name glo4000.mydomain.com www.glo4000.mydomain.com;


    access_log  /var/log/nginx/glo-4000.access.log;
    error_log /var/log/nginx/glo-4000.error_log;

    location / {
        root   /home/ul/glo-4000/;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {

        include fastcgi_params;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /home/ul/glo-4000/$fastcgi_script_name;
    }
}

抱歉...没什么用...我试着在服务器上添加了上面的那一行并将尾部的 / 删除了...这是Z2F模块,从这个URL调用,其中没有索引...请帮忙。 - Poonam Bhatt
好的,这是一些重要信息。你看过这个吗:https://dev59.com/PmIj5IYBdhLWcg3w8ZYF - Johnride
你是否遇到了不同的错误代码?你具体在尝试什么?你是否尝试使用套接字而不是 127.0.0.1:9000?你是否尝试在 location / 语句中声明你的根目录,并使用 try files 命令?你的 PHP-CGI 日志中有任何错误吗?你的 NGinx 日志中有更详细的错误信息吗? - Johnride
我无法使用socket,因为我找不到socket文件。是的,我在location /中添加了root和try_files $uri $uri/ /index.php;。我的access.log显示'"GET /webvalue-services/public/testservice/ HTTP/1.1" 404 36 "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"'。我不知道如何检查Nginx和php-cgi日志? - Poonam Bhatt
在位置块中不应该有root指令:http://nginxlibrary.com/resolving-no-input-file-specified-error/ - djthoms
php位置块无法访问同级的根指令。根应该在父级或php位置块内部。 - Agnel Vishal

2

我尝试了上面提到的所有选项,但最终找到了解决方案。 在我的服务器上,.php文件被设置为任何人都可以读取,但当我将php-fpm设置为与nginx运行在同一用户下时,它就起作用了。我在/etc/php/7.2/fpm/pool.d/www.conf中进行了更改,并在配置文件中设置了

user = nginx group = nginx

然后重新加载了php-fpm进程。 希望这能帮到你。


1

好的,我假设您正在使用Ubuntu 16或更高版本上的php7.2(或更高版本)

如果这些都不起作用,您必须知道nginx-fastCGI在同一服务器上托管的不同站点使用不同的pid和.sock。

要解决“未指定输入文件”的问题,您必须告诉nginx使用哪个sock文件来使用您的yoursite.conf文件。

  1. Uncomment the default fastcgi_pass unix:/var/run/php7.2-fpm.sock Make sure you have the following directives in place on the conf file,

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
            try_files $uri /index.php =404;
            #fastcgi_pass unix:/var/run/php7.2-fpm.sock;
            fastcgi_pass unix:/var/php-nginx/158521651519246.sock/socket;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
    
  2. have a look at the list of sock and pid files using ls -la /var/php-nginx/(if you have recently added the file, it should be the last one on the list)

3. 复制 .sock 文件的文件名(通常是一个 15 位数字),并将其粘贴到您的 location ~ \.php 指令中。

fastcgi_pass unix:/var/php-nginx/{15digitNumber}.sock/socket;

然后重新启动 nginx。如果有效,请告诉我。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接