nginx + php-fpm = 文件未找到

24

当我尝试访问info.php时,我会收到一个文件未找到的错误。

我尝试了一些教程但都没有成功。

配置信息: 默认:

server {
    listen         80;
    listen   [::]:80 default ipv6only=on; 
    server_name  localhost;

    location / {
        root   /var/www;
        index  index.html index.htm index.php;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:7777;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
        fastcgi_buffers 256 128k;
        #fastcgi_buffer_size 16k;
        #fastcgi_busy_buffers_size 256k;
        fastcgi_connect_timeout 300s;
        fastcgi_send_timeout 300s;
        fastcgi_read_timeout 300s;
        include fastcgi_params;
    }
}

有什么问题吗?


你的文档根目录中有 info.php 文件吗? - Felix Frank
9个回答

19
如果info.php文件在/var/www中,那么指示fast_cgi查找是错误的。
/usr/share/nginx/html/info.php;

使用相同的根目录来处理html和php文件。同时,rootindex参数应该放在特定位置之外,除非有非常具体的用途。

server {
   listen         80;
   listen   [::]:80 default ipv6only=on; 
   server_name  localhost;
   root   /var/www;
   index  index.html index.htm index.php;


   #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

   location ~ \.php$ {
       fastcgi_pass 127.0.0.1:7777;
       fastcgi_index  index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_buffers 256 128k;
       fastcgi_connect_timeout 300s;
       fastcgi_send_timeout 300s;
       fastcgi_read_timeout 300s;
       include fastcgi_params;
    }
}

不用说,你仍然需要确保你的php-fpm服务正在监听端口7777。通常情况下,它是监听在9000端口。


7
你可以写$document_root$fastcgi_script_name而不是/var/www$fastcgi_script_name,这样可以避免复制粘贴的小错误。 - Oleg
3
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 这让我受益匪浅,谢谢。 - herbertD

15

如果您已经检查了所有内容并正确配置了,那么我得到的最后一点是:

  • 检查在文件 /etc/php-fpm.d/www.conf 中是否提到了正确的用户名

3
在 OS X Yosemite 上通过 Homebrew 安装了 PHP 5.6,出现了这个问题。这个问题很棘手,因为 Nginx 错误日志和 php-fpm 错误日志,在“调试”模式下都没有提供比“主要脚本未知”更详细的信息。默认的用户/组是 _www:_www,可能不是您想要的(我使用[我的名字]:staff)。要重新启动,请使用 sudo /usr/local/opt/php56/sbin/php56-fpm restart - William Turrell
这帮了我,之前浪费了一个小时寻找解决方案。谢谢。 - Sahil
拯救了我的生命。非常感谢。 - RoyHu
感谢。工作得很好。更改了 user = nginxgroup = nginx - Hikmat Sijapati

2
在nginx的location部分中,如果您使用root,那么$document_root$fastcgi_script_name的值是正确的。但是如果您使用alias,则该值是错误的。当使用alias时,您需要使用$request_filename而不是$document_root$fastcgi_script_name
location /myapp {
    alias /home/site/www/myapp;
    location ~* "\.php$" {
        fastcgi_pass   php_fpm_server:9000;
        fastcgi_index  index.php;

        # Works in all cases.
        fastcgi_param  SCRIPT_FILENAME $request_filename;

        ## Not work for 'alias' directive, only for 'root' directive.
        # fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

上述变量的文档:

$request_filename

基于root或alias指令和请求URI的当前请求文件路径

$document_root

当前请求的root或alias指令的值

$fastcgi_script_name

请求URI或者如果URI以斜杠结尾,则将fastcgi_index指令配置的索引文件名附加到请求URI。此变量可用于设置决定PHP脚本名称的SCRIPT_FILENAME和PATH_TRANSLATED参数。例如,对于以下指令的“/info/”请求

阅读更多


1
server {
listen         80;
listen   [::]:80 default ipv6only=on; 
server_name  localhost;
root   /var/www;
location / {
    index index.php;
}
location ~ \.php(?:$|/) {
   fastcgi_pass 127.0.0.1:7777;
   fastcgi_index  index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_buffers 256 128k;
   fastcgi_connect_timeout 300s;
   fastcgi_send_timeout 300s;
   fastcgi_read_timeout 300s;
   include fastcgi_params;
}

}


1

Nginx和符号链接

注意,如果root /var/www;是一个符号链接,请将其更改为root /var/www/;否则nginx将无法通过符号链接并且您将收到文件未找到错误。


0

我曾经遇到过这个问题,但这里的所有答案都没有起作用。最终,我发现问题是由SELinux引起的。我禁用了SELinux,问题得到了解决。

请注意,禁用SELinux会对安全性产生影响。肯定有更好的方法来解决这个问题,但禁用SELinux对我的目的有效。


0

我仔细地查看了这个问题,最后发现 FPM 没有运行 :-s 在我的情况下,简单的服务 php7.2-fpm restart 就解决了问题!


0
唯一有效的方法是卸载整个 PHP 版本,删除所有配置文件,然后重新安装 PHP 版本。

0

在尝试解决类似问题时,我遇到了这个。因此,在我找到解决方案时,我将添加我发现的解决方案。这是在Arch上,但它与systemd有关。

此解决方案适用于我的开发机器,由于某些重要原因,您不应该从/home文件夹运行公共站点。

我配置了php-fpm和nginx以以我的用户身份运行。编辑以下文件并删除ProtectHome=true行:

sudo vi /etc/systemd/system/multi-user.target.wants/php-fpm.service

重新加载并重启所有内容;

systemctl daemon-reload
systemctl restart nginx.service
systemctl restart php-fpm.service

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