Nginx与PHP-FPM环境下的访问故障现象
问题场景:
- 运行环境为Linux系统。
- Web服务器采用Nginx。
- 已安装并配置好PHP7的FPM模块。
- 在Nginx的Web根目录下创建了index.php文件,文件中仅包含phpinfo()函数调用。(若通过源码编译安装,该目录通常位于/usr/local/nginx/html/index.php)。
对Nginx进行了基础配置,旨在测试其是否具备处理PHP请求的能力。
location / {
root html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
在浏览器地址栏输入 localhost/index.php 进行访问测试。
页面返回结果为 File not found.。
此时检查Nginx的错误日志,主要报错信息为 FastCGI sent in stderr: "Primary script unknown"。
结合分析结果与前人经验,得出结论:Nginx配置文件未能正确识别 /scripts 这一路径。因此,需将配置项中的 /scripts 替换为 $document_root,或者直接使用Web目录的绝对路径。修改后的相关配置片段如下所示:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
重启Nginx服务后,页面能够正常显示 phpinfo() 的输出内容。
总结
以上就是本文的全部内容,希望这些内容能对各位读者在学习或工作中有所帮助。如有任何疑问,欢迎在下方留言交流。