欢迎来到代码驿站!

当前位置:首页 >

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;
	}

上一篇:Ubuntu上PHP的安装以及版本切换

栏    目:

下一篇:R语言ggplot2边框背景去除的实现

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

本文地址:http://www.codeinn.net/misctech/2315.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有