优化php-fpm解决Nginx 502 Bad Gateway

netstat -anpo | grep  php-cgi  | wc -l

用上面命令查看FastCGI进程数是否够用,如果此数值接近预设值,那么,说明FastCGI进程数设置的过小。

打开/usr/local/php/etc/php-fpm.conf

修改参数如下:

listen = /tmp/php-cgi.sock
user = www
group = www
pm = dynamic
pm.max_children = 100   #静态php-fpm进程,固定
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 100  #动态最大php-fpm进程
request_terminate_timeout = 150  #超时终止


php-fpm三种模式static , dynamic , ondemand
static 固定数量进程
dynamic 动态进程,但是释放缓慢,比较占内存
ondemand 限时主动释放进程,省内存

pm = ondemand          # ondemand模式适用
pm.max_children = 100   #静态php-fpm进程,固定,
pm.start_servers = 10   # 初始进程,一个进程大概20M,注意计算内存
pm.min_spare_servers = 10  # 动态最小php-fpm进程
pm.max_spare_servers = 100  #动态最大php-fpm进程,需要小于pm.max_children
request_terminate_timeout = 300 #超时终止
pm.process_idle_timeout = 60     #ondemand模式,主动释放进程
pm.max_requests = 10240           #达到请求数释放,防止内存泄漏

Leave a Comment