出现“File not found.”的核心原因是fastcgi_param SCRIPT_FILENAME配置错误,导致PHP-FPM找不到对应的PHP文件。以下是具体分析和修复方案:
一、直接原因:SCRIPT_FILENAME路径错误
你配置中的这一行:
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;是Nginx默认的错误写法——/scripts是一个不存在的目录,而你的PHP文件实际在/app/code/zbx目录下。
当Nginx把请求转发给PHP-FPM时,会告诉它“要执行的文件在/scripts/xxx.php”,但这个路径根本不存在,所以PHP-FPM返回“File not found.”。
二、修复步骤
1. 修正SCRIPT_FILENAME参数
将/scripts改为$document_root(自动关联当前配置的root目录),同时补充root和try_files指令(确保路径正确):
location ~ \.php$ { root /app/code/zbx; # 必须和上面location /的root一致,指定PHP文件所在目录 try_files $uri =404; # 先检查文件是否存在,不存在直接返回404(避免无效请求) fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # 关键修正:用$document_root获取当前root目录,正确指向PHP文件 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }2. 补充index的优先级(可选但建议)
将index中的index.php放到前面,优先解析PHP索引页:
location / { root /app/code/zbx; index index.php index.htm; # 把index.php放前面 }三、修复后的完整配置
server { listen 80; server_name localhost www.zbx888.com; location / { root /app/code/zbx; index index.php index.htm; # 优先解析PHP索引页 try_files $uri $uri/ /index.php$is_args$args; # 处理PHP项目路由 } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # 修复后的PHP解析块 location ~ \.php$ { root /app/code/zbx; # 和项目根目录一致 try_files $uri =404; # 检查文件是否存在 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # 禁止访问隐藏文件(提升安全性) location ~ /\.ht { deny all; } }四、验证与生效
检查配置语法:
nginx -t输出“test is successful”说明语法正确。
重启Nginx:
systemctl restart nginx确认PHP文件存在:
确保/app/code/zbx目录下有index.php(或你访问的PHP文件):ls-l /app/code/zbx/index.php
五、额外排查(若仍报错)
如果修复后还是“File not found.”,检查:
- PHP-FPM的用户权限:PHP-FPM默认以
apache或nginx用户运行,需确保该用户能读取/app/code/zbx下的文件:chown-R nginx:nginx /app/code/zbxchmod-R755/app/code/zbx - SELinux限制:临时关闭SELinux测试(生产环境需配置规则):
setenforce0
修复后,浏览器访问www.zbx888.com就能正常解析PHP文件了。