1.配置错误
因为nginx找不到php-fpm了或者socket文件权限不对,所以报错,一般是nginx的配置文件中fastcgi_pass后面的路径配置不当,后面可以是socket或者是ip:port,从php5.4版本之后如果使用socket方式,默认权限给的比较低,所以需要把监听权限改为777,对应配置为 listen.mode=0777 2.资源耗尽
lnmp架构在处理php时,nginx直接调取后端的php-fpm服务,如果nginx的请求量偏高,我们又没有给php-fpm配置足够的子进程,那么php-fpm就会资源耗尽,一旦资源耗尽nginx找不到php-fpm就会出现502错误 解决方案:
去调整php-fpm.conf中的pm.max_children数值,使其增加,但是也不能无限增加,毕竟资源有限,一般4G内存机器如果跑php-fpm和nginx,不跑mysql可以设置为150,8G为300以此类推! 3.除了上面的两种错误还有其他的原因很少有,我们可以借助nginx的错误日志来进行排查 vim /usr/local/nginx/logs/nginx_error.log 我们也可以给日志定义级别vim/usr/local/nginx/conf/nginx.conf 找到error_log,默认是crit最严谨的就行,也可以改成debug显示的信息最全面,但是很容易撑爆我们的磁盘。 如下为:nginx解析php相关配置 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
| [iyunv@chy01 vhost]# vim test.com.conf
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com/$fastcgi_script_name;
}
(如上在虚拟主机里面配置的php解析,需要注意几个地方1fastcgi_pass unix:/tmp/php-fcgi.sock; 这里的路径不要写错,如果写错会出现502问题,
[iyunv@chy01 conf]# cat /usr/local/php-fpm/etc/php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock (这个路径要与nginx虚拟主机的路径完全保持一致)
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
还有另一种情况就是php监听的不是sock,而是ip加端口的情况时
location ~ \.php$
{
include fastcgi_params;
#fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com/$fastcgi_script_name;
}
这时fastcgi_pass 127.0.0.1:9000 这里就需要这样操作,这的ip要与php配置文件里面监听的地址保持一致)
第2个需要注意的地方:fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com/$fastcgi_script_name; 这里的路径要与server 下的
{
listen 80;
server_name test.com test2.com lll.com;
index index.html index.htm index.php admin.php;
root /data/wwwroot/test.com; 这个路径保持一致。
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
3.php5.4之后的版本有个特性:
[iyunv@chy01 vhost]# vi /usr/local/php-fpm/etc/php-fpm.conf (在配置文件中)
listen = /tmp/php-fcgi.sock
listen.mode = 666
(如果监听的是sock就必须要监听mode)
#listen.mode = 666
[iyunv@chy01 vhost]# ls -l /tmp/php-fcgi.sock
srw-rw---- 1 root root 0 8月 15 03:02 /tmp/php-fcgi.sock
(可以看到属主属组都为root,并且没有读的权限)
[iyunv@chy01 vhost]# curl -x127.0.0.1:80 test.com/admin.php -I
HTTP/1.1 502 Bad Gateway
Server: nginx/1.12.1
Date: Mon, 14 Aug 2017 19:03:48 GMT
Content-Type: text/html
Content-Length: 173
Connection: keep-alive
(当在curl时会发现报502)
[iyunv@chy01 vhost]# tail -n3 /usr/local/nginx/logs/nginx_error.log
2017/08/14 22:24:06 [crit] 3684#0: *7 connect() to unix:/php-fcgi.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/php-fcgi.sock:", host: "test.com"
2017/08/15 03:03:48 [crit] 3682#0: *5 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"
2017/08/15 03:06:03 [crit] 3682#0: *7 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/admin.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"
(查看错误日志,Permission denied发现拒绝,没有权限。是因为它的属主,属组为nobody ,此时如果将/tmp/php-fcgi.sock 的权限设置为nobody就没有任何问题了)
[iyunv@chy01 vhost]# ps aux |grep nginx
root 3485 0.0 0.1 21272 1652 ? Ss 02:28 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 3682 0.0 0.2 23152 3796 ? S 03:01 0:00 nginx: worker process
nobody 3683 0.0 0.2 23152 3300 ? S 03:01 0:00 nginx: worker process
root 3791 0.0 0.0 112664 972 pts/0 S+ 03:11 0:00 grep --color=auto nginx
(还有一种情况也是502就是nginx的资源耗尽))
|
希望看过的童鞋多多指教,谢谢!
|