nginx+php-fpm 报错Primary script unknown

报错信息(nginx日志):

2019/01/04 13:30:52 [crit] 38907#0: *181 stat() "/home/xiaoju/webroot/c-api-intl/app/htdocs/user/getUserInfo" failed (13: Permission denied), client: 172.21.205.25, server: localhost, request: "GET /user/getUserInfo HTTP/1.1", host: "10.96.83.130:8005"
2019/01/04 13:30:52 [crit] 38907#0: *181 stat() "/home/xiaoju/webroot/c-api-intl/app/htdocs/user/getUserInfo" failed (13: Permission denied), client: 172.21.205.25, server: localhost, request: "GET /user/getUserInfo HTTP/1.1", host: "10.96.83.130:8005"
2019/01/04 13:30:52 [error] 38907#0: *181 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.21.205.25, server: localhost, request: "GET /user/getUserInfo HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "10.96.83.130:8005"

意思就是找不到php的入口脚本,那么查看nginx配置是否有问题,主要关注以下配置中标红的部分:

 location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  ENV_CAPI_DEBUG true;
        fastcgi_param  APP_ENV development;
        include        fastcgi_params;
    }

我们也可以加nginx日志来验证 SCRIPT_FILENAME 是否配置正确:

# add to nginx.conf
log_format scripts '$document_root$fastcgi_script_name > $request';

# add to server conf block
access_log /var/log/nginx/scripts.log scripts;

做好以上配置之后重启nginx并请求,同时tail -f /var/log/nginx/scripts.log,查看输出的php脚本能不能找到。

我这里输出是这样的:

/home/xiaoju/webroot/c-api-intl/app/htdocs/index.php > GET /user/getUserInfo HTTP/1.1

如果找不到该文件,重新配置nginx,保证php入口脚本能被找到。

如果已经找到该文件,而且还报此错误,那就是权限问题了:运行php-fpm的linux用户没权限访问php入口文件的权限。

我这里运行php-fpm的用户是nginx。(在/usr/local/php/etc/php-fpm.conf配置文件中配置的)

sudo -u nginx stat /home/xiaoju/webroot/c-api-intl/app/htdocs/index.php

果然权限不足,解决办法:

chown nginx:nginx /home/xiaoju/webroot/c-api-intl
chmod g+x /home
chmod g+x /home/xiaoju
chmod g+x /home/xiaoju/webroot

问题解决。

如果中途出现:account is currently not available

vipw /etc/passwd

把对应用户的/sbin/nologin 改成 /bin/bash。

参考文档:

https://www.jianshu.com/p/ce968818497b

https://www.e-learn.cn/content/wangluowenzhang/299721

https://blog.csdn.net/shaobingj126/article/details/7466583