php-fpm配置不当,导致服务器经常出现502错误,上个学期多次调整都没有解决,网上找来资料,大都是增加max_children,可是我都加到顶了,php-fpm log里面还是有大量的警告:

eems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 0 idle, and 88 total children

不想看英文,一看就头晕,但是502更头晕,特别是昨天,刚重启了lnmp没多久,容不得松口气,就看到负载飙到20多,一开网站,又特么502了。

硬着头皮看默认配置里面的英文注释,才发现自己的三个参数配置貌似有相当大的问题:

Choose how the process manager will control the number of child processes.
; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives. With this process management, there will be
;             always at least 1 children.
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;             pm.start_servers     - the number of children created on startup.
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
;  ondemand - no children are created at startup. Children will be forked when
;             new requests will connect. The following parameter are used:
;             pm.max_children           - the maximum number of children that
;                                         can be alive at the same time.
;             pm.process_idle_timeout   - The number of seconds after which
;                                         an idle process will be killed.
; Note: This value is mandatory.

 

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI. The below defaults are based on a server without much resources. Don't
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 5
; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 2
; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 1
; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 3

 

注释上貌似是说,static模式时,只有pm.max_children生效,dynamic模式时,pm.max_children依然生效,另外:

pm.start_server  #控制服务启动时创建的进程数,

pm.min_spare_servers  #控制最小备用进程数

pm.max_spare_servers  #最大备用进程数

spare_servers翻译成备用进程,不知道合适不适合,如果真这样,那我之前的配置就让人无奈了: pm.min_space_servers 20,pm.max_spare_servers 80,pm.max_children 80,会不会是因为备用的太多才导致502呢?

 

另外,下面的具体配置里面,注释中给出了计算pm.start_servers默认值的公式:

Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2

按这个公式,我之前的pm.start_servers应该是 20+(80-20)/2=50,可是我当时设置的是20…

重新规划如下:

pm = dynamic
pm.max_children =80     备:我的值为360
pm.start_servers = 12       我设为了190
pm.min_spare_servers = 4    我设80
pm.max_spare_servers = 20   我设300
rlimit_files = 65535
pm.max_requests = 102400

 

貌似要好点了,内存使用不像之前那么夸张了:

php-fpm配置

 

================================>

在  /etc/sysctl.conf 最末增加了一行:

#add by kevin at 201505172115
fs.file-max=65536

============== 调优及排错部份====================》》

 

php-fpm错误导致网站报502错误

 

05

 

我的网站运行环境是Nginx +php fastcgi模式的。这几天运行一直不稳定,总是出错,报502错误。
今天跟以前的同事请教了一下,他告诉我检查一下php-fpm的日志,那里记录了很多有用的信息。
于是我检查了一下,发现确实有很多报错信息:
Mar 01 14:40:01.922284 [NOTICE] fpm_got_signal(), line 48: received SIGCHLD
Mar 01 14:40:01.922343 [NOTICE] fpm_children_bury(), line 215: child 12057 (pool default) exited with code 0 after 170.403416 seconds from start
Mar 01 14:42:12.839544 [NOTICE] fpm_children_make(), line 352: child 12962 (pool default) started
Mar 01 14:42:16.817386 [NOTICE] fpm_got_signal(), line 48: received SIGCHLD
Mar 01 14:42:16.817449 [NOTICE] fpm_children_bury(), line 215: child 12385 (pool default) exited

[root@localhost logs]# tail -50 /www/wdlinux/php/logs/php-fpm.log
Mar 05 13:11:55.321376 [NOTICE] fpm_children_make(), line 352: child 10657 (pool default) started
有的报错信息,就好说了,直接上网查信息。
经过搜索,最后总结出以下几条优化策略:

1、提升服务器的文件句柄打开打开

# vi /etc/security/limits.conf 加上
* soft nofile 65500
* hard nofile 65500

2、提升nginx的进程文件打开数

nginx.conf : worker_rlimit_nofile 51200;

3、修改php-fpm.conf文件,主要需要修改2处。

命令 ulimit -n 查看限制的打开文件数,php-fpm.conf 中的选项rlimit_files 确保和此数值一致。

10240

65500

4、
# vi /etc/sysctl.conf
底部添加
fs.file-max=65500

后 sysctl -p 生效。


经过以上修改,重启PHP。
到目前为止还没有出现过以上的报错信息。一切运行正常。