nginx+php出现No input file specified解决办法
2022-07-28 15:09:43
135
{{single.collect_count}}

今天在自己本地的开发环境突然出现了No input file specified错误,反复检查返现自己的配置文件和配置路径以及权限都没有问题。经过反复的排查终于发现了问题,现将问题及解决分享如下:

问题原因分析

在GitHub上下载了一个开源的tp5项目,之前自己本地的网站运行都没有问题。但是安装了这个开源项目后就发现本地其他网站都无法访问了。访问就是No input file specified错误。在网上也找了解决办法,但是都不是,看来这个错误有点儿诡异。
后来反复尝试,重启电脑后问题得到解决但是再次运行下载的tp5开源项目后其他网站又出现了这样的错误No input file specified 而且只有这一个网站运行没有问题。
据此将错误圈定在该开源项目的nginx配置文件中。再来看看该配置文件:

server {listen 80;server_name local.test.com;access_log /data/wwwlogs/local.test.com.log combined;error_log /data/wwwlogs/local.test.com_error.log error;index index.html index.htm index.php;root /data/php/test;add_header X-Powered-Host $hostname;fastcgi_hide_header X-Powered-By;if (!-e $request_filename) {rewrite^/(.+?\.php)/?(.*)$/$1/$2last;rewrite^/(.*)$/index.php/$1last;}location ~ \.php($|/){fastcgi_index index.php;fastcgi_pass127.0.0.1:9000;include fastcgi_params;set $real_script_name $fastcgi_script_name;if ($real_script_name ~ "^(.+?\.php)(/.+)$") {set $real_script_name $1;}fastcgi_split_path_info ^(.+?\.php)(/.*)$;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param SCRIPT_NAME $real_script_name;fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;fastcgi_param PHP_VALUE open_basedir=$document_root:/tmp/:/proc/;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {access_logoff;error_log off;expires 30d;}location ~ .*\.(js|css)?$ {access_log off;error_logoff;expires12h;}

在以上的配置中其他都是常规的配置。因为我使用cgi。在fastcgi参数中有一行可能大家也注意到了。

fastcgi_param PHP_VALUE open_basedir=$document_root:/tmp/:/proc/;

就是这句。这句的主要作用是设置fastcgi的可操作目录从而防止跨站的,将open_basedir限定在了本项目的目录和/tmp/以及/proc/中。

问题解决

刚刚说了是在配置的fastcgi配置中多了一句防止跨站的语句。那么这句话他其实是影响了整个fastcgi的参数,这样因为我的其他网站的路径是/data/php/xxx/这样的目录,而不在本开源项目的目录/data/php/test/所以fastcgi就无法找到。
所以在这句之前加#注释这句或者删除这句重启系统或重启nginx就可以了。

线上部署的建议

那么到底要不要使用这句呢?在线上环境中当然是可以的。在线上项目部署中对于open_basedir中最好别使用$document_root这样的变量。如果有多个项目在线上服务器中那么可以把所以项目放置在一个统一的目录中。例如我的线上是wwwroot目录下放置其他网站。例如/wwwroot/test1 /wwwroot/test2那么我可以配置为

fastcgi_param PHP_VALUE open_basedir=/wwwroot/:/tmp/:/proc/;
回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}