如何解决 Nginx 与 FastCGI 通信错误 "Primary script unknown"?
错误原因
不少小伙伴在搭建好 LNMP 环境后,进行测试时,在测试页会出现 File not found.
的提示信息。查看错误日志报告显示 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
这样的错误信息。之所以出现这样的情况,是因为 Nginx
将请求 uri
转发给 FastCGI
处理时, FastCGI
并没有接收到相应的 uri
参数。出现这样的错误大致分为以下三种。
采用默认配置信息
在安装好 Nginx
并配置好根目录之后,采用了默认的 SCRIPT_FILENAME
配置参数,这样的话 FastCGI
无法定位到正确目录解析 php
文件。
默认配置
location ~ \.php$ {
root /home/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
改为
location ~ \.php$ {
root /home/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
$document_root
表示 root
指定的目录,针对 /home/web
目录下的 php
文件进行解析。
根目录配置错误
如果你的根目录地址是 /home/web
,实际配置成 /home/www
,也会出现同意的错误信息,将错误地址改回正确地址即可。
Nginx
用户与 Php-fpm
用户不一致
如果以上配置都为正确的情况下,将 nginx.conf
中的 user
用户配置与 www.conf
用户配置设置为统一的用户与用户组。
在 nginx.conf
中
http {
user nginx nginx;
worker_processes 8;
...
}
在 Php-fpm
的 www.conf
中
user = nginx
group = nginx
如果没有 nginx
用户,创建这个用户
useradd nginx
本作品采用《CC 协议》,转载必须注明作者和本文链接