nginx + php-fpm =文件未找到

sshcrbum  于 2022-12-03  发布在  Nginx
关注(0)|答案(9)|浏览(204)

当我尝试访问info.php时,出现File not found.错误。
我尝试了一些教程,但没有效果。
配置:默认:

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;
    }
}

有什么问题吗?

ar7v8xwq

ar7v8xwq1#

如果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。

gv8xihay

gv8xihay2#

如果你检查了每件事,它的正确配置,然后有最后一点我得到:

  • 如果在文件/etc/php-fpm.d/www.conf中提及,请检查用户名是否正确
iq0todco

iq0todco3#

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;
}

}

fslejnso

fslejnso4#

Nginx和符号链接

注意,如果root /var/www;是一个符号链接,请将其更改为root /var/www/;,否则nginx不会通过符号链接,您将得到File not found错误

xxslljrj

xxslljrj5#

location nginx区段中,如果您使用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;
    }
}

上述变量的文档:

$请求文件名

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

$文档根

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

$fastcgi脚本名称

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

阅读更多

hgc7kmma

hgc7kmma6#

我花了很长时间看这个,最后发现FPM没有运行:-s在我的例子中,一个简单的服务php7.2-fpm重新启动完成了这个任务!

eufgjt7s

eufgjt7s7#

我遇到了这个问题,这里的答案都不起作用。对我来说,这个问题最终是由SELinux. I disabled SELinux引起的,它修复了这个问题。
请注意,禁用SELinux确实会对安全性产生影响。几乎可以肯定有更好的方法来解决这个问题,但禁用SELinux对我来说是有效的。

kuuvgm7e

kuuvgm7e8#

我在尝试解决一个类似的问题时遇到了这个问题。所以我会把我找到的解决方案加进去。这是在Arch上,但它是与systemd相关的。
此解决方案适用于我的开发计算机,出于充分的理由,您不应该从/home文件夹运行公共站点。
我将php-fpm和nginx配置为以我的用户身份运行。

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

重新加载,并重新启动一切;

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

b4lqfgs49#

唯一对我有效的是卸载整个版本的php,删除它所有的配置文件,然后再次重新安装php版本。

相关问题