环境: docker 中 centos 镜像下 yum 安装的php,nginx。
[root@lnmp1 /]# php -v
PHP 7.2.11 (cli) (built: Oct 9 2018 15:09:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
[root@lnmp1 /]# nginx -v
nginx version: nginx/1.14.1
php-fpm 的配置文件路径: /etcphp-fpm.d/www.conf
nginx 的配置文件路径 : /etc/nginx/nginx.conf
php-fpm和nginx的两种通信方式:
1,tcp socket。即 ip:port 方式。(适用于php和nginx在不同的主机上)
php-fpm 的配置文件中这样 listen=127.0.0.1:9000; #一般都是9000端口,如果不写ip,就是允许从其它机器可以访问。
nginx 的配置文件中 fastcgi_pass 127.0.0.1:9000; #这个在location 块中,表示指向的ip:port,要指向php-fpm的地址。
2,unix socket daemon (只能当nginx和php在同一主机上时使用,效率比上一种高)
php-fpm 的配置文件中这样 listen=/var/run/php-fpm/www.socket; # socket 文件的名字路径都要一样
nginx 的配置文件中 fastcgi_pass /var/run/php-fpm/www.socket;
3,nginx 配置php文件解析时,注意 location 块里面的root 路径是php主机上的目录,而不是 nginx主机里的。
location ~ \.php$ {
root /html; #这是php-fpm服务器上的地址,并不是Nginx的本地地址。
fastcgi_pass 172.17.0.3:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
1,错误一: 配置好了之后,使用PHPinfo() 函数,使用index.php 测试,不要傻乎乎的用 index.html 测试了,白白浪费了那么多时间,还tm伤脑筋。
2,错误二:检查两种通信方式有没有弄混。不然会报错,(111: Connection refused)。
3,报错: [error] 219#0: *67 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,
解决:修改 fastcgi_param 后面的参数。 原因是因为 已经配置了 root 路径。
修改前: fastcgi_param SCRIPT_FILENAME
/scripts
$fastcgi_script_name;
修改后 : fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
查看下面这篇文章更详细。
https://www.cnblogs.com/smallrookie/p/7491514.html
总结:必须查看报错信息 /var/log/nginx/error.log