当前位置:主页 > >

Linux下搭建nginx+php环境的file not found(Primary script unknown)问题排查

时间:2020-06-12 23:52:35 | 栏目: | 点击:

相信很多配置php环境的小伙伴都遇到过这样的问题:
浏览器访问php文件,返回来 File not found,但是静态文件html是能够访问的,所以还是php的配置没有配置好。

查看配置的日志/var/log/nginx/error.log,有 “Primary script unknown”,类似如下:

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,

一般情况下,原因只有两个,一个是php-fpm找不到php文件,一个是php-fpm没有权限读取和执行文件。

    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

权限问题,也是坑最多的。
1) 进程用户
nginx.conf 里的 user 配置要跟 php-fpm.d/www.conf一致,比如都用 nginx,或者自定义用户,默认是www-data。
nginx.conf :

user www-data;
worker_processes auto;
php-fpm.d/www.conf :
user = www-data
group = www-data
然后再查看ps -ef | grep php-fpm和ps -ef | grep nginx,两个的用户是一样的,访问成功了。

2) 目录和文件权限
php文件不必非得设为 777,让人怪担心的,只要是nginx和php-fpm运行用户可读写执行即可,一般可以770 。
php文件目录和文件样例:
drwxrwx--- 6 www-data www-data 4.0K 2019-01-03 13:09 /usr/share/nginx/html
-rwxrwx--- 1 www-data www-data 40   2019-01-03 13:09 /usr/share/nginx/html/phpinfo.php
测试方法:
sudo -u www-data ls -l /usr/share/nginx/html/
还有一种坑就是,配置域名的站点时,location下root一定要写,有时候分开配php时,经常漏掉:
	location / {
		root /www/vita_web/;
	}
	location ~ .*\.php$ {
	  root /www/vita_web/;
	  fastcgi_pass   127.0.0.1:9001;
	  fastcgi_index  index.php;
	  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	  include        fastcgi_params;
	}

您可能感兴趣的文章:

相关文章