php, nignx 'No input file specified'

FastCGI模式下访问php文件时,出现 No input file specified 错误。接口返回状态码 404

查看access.log 发现是错误信息如下

2019/08/12 18:42:58 [error] 10023#0: *259 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: open_basedir restriction in effect. File(/home/wwwroot/xx/public/index.php) is not within the allowed path(s): (/home/wwwroot/default/:/tmp/:/proc/) in Unknown on line 0
PHP message: PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0
Unable to open primary script: /wwwroot/xx/public/index.php (Operation not permitted)" while reading response header from upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /api/msg/list?pageSize=10&page=0&content= HTTP/1.0", upstream: "fastcgi://unix:/tmp/php-cgi.sock:", host: "localhost:8010", referrer: "https://x.qxxin.com/operation"

原因分析:

  1. 任何对.php文件的请求,都简单地交给php-cgi去处理,但没有验证该php文件是否存在。 PHP文件不存在,没办法返回普通的404错误,它返回一个404,并带上一句 "No input file specified"。
  2. 还可能跟路径或者权限有关系。或者SCRIPT_FILENAME变量没有被正确的设置(这在nginx是最常见的原因)
    • 如果html也出现404错误,那么就是document root 设置的有问题
    • 检查脚本文件的权限, 可能PHP或者web server不能读取它
    • SCRIPT_FILENAME设置错误

根据 open_basedir restriction in effect. not within the allowed path(s): (/home/wwwroot/default/:/tmp/:/proc/) 发现是 open_basedir 出现了问题。

open_basedir 限制了php进程不能读取open_basedir 设置的目录之外的文件。这个设置主要是为了服务器安全。

php.ini中配置好open_basedir 就可以解决该问题

[root@zctcgeagle01 etc]# grep -rn 'open_basedir' ./
./php.ini:305:; open_basedir, if set, limits all file operations to the defined directory
./php.ini:309:open_basedir = /home/wwwroot/:/home/wwwroot/default/:/tmp/:/proc/