Nginx报"no input file specified"错误解决
前言
访问 PHP 页面,直接报 "No input file specified",意思是 PHP-FPM 找不到要执行的 PHP 文件。这是 Nginx 配置里 root 路径不对导致的。
报错长什么样
页面上直接显示:
No input file specified.
没有其他信息,就这么一句话。
常见原因
原因1:root 路径不对
PHP location 里的 root 和 server 里的 root 不一致。
错误示例:
server {
root /var/www/html;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME /var/www/php$fastcgi_script_name;
}
}
这样 PHP 找文件的时候就找错目录了。
正确做法:
server {
root /var/www/html;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
用 $document_root 自动取 server 里的 root。
原因2:文件真的不存在
URL 对应的 PHP 文件根本就没有。
检查:
ls /var/www/html/test.php
原因3:文件权限不对
文件存在,但 Nginx/PHP-FPM 读不了。
解决: 确保文件权限是 644,目录是 755,属主是 www。
完整正确的配置
server {
listen 80;
server_name www.example.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
排查步骤
第一步:确认文件在不在
ls /var/www/html/test.php
第二步:看 Nginx 错误日志
tail -f /var/log/nginx/error.log
访问一下报错的页面,看日志里写的路径是什么。
第三步:确认 root 路径
grep root /etc/nginx/conf.d/你的站点.conf
常见坑
坑1:fastcgi_param 写错了
写成了 $request_filename 或者其他变量。
解决: 一定要用 $document_root$fastcgi_script_name。
坑2:伪静态和 PHP 冲突
配了伪静态,导致 PHP 请求被重写到别的地方了。
解决: 确保伪静态规则在 PHP location 之前,而且 PHP location 能正常匹配到 .php 请求。
坑3:多站点 root 不一致
A 站点的 PHP 请求跑到 B 站点去了。
原因: 多个站点的 root 配置不对。
解决: 每个站点的 root 都写清楚,别共用。
坑4:PHP-FPM 配置不对
PHP-FPM 的 www.conf 里的 listen 路径和 Nginx 配置的不一致。
解决: 确认两边的 socket 路径一样。
总结
90% 的情况都是 root 路径不对。
检查这三点:
- root 路径写对了没
- 文件真的存在不
- 文件权限对不对
记住: PHP 配置里一定要用 $document_root$fastcgi_script_name,别写死路径。
遇到问题加QQ23979811 协助处理