44

错误解决:Nginx php-fpm 502 bad gateways

 5 years ago
source link: https://www.linuxprobe.com/502-bad-gateways.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

环境:

Ubuntu 16.04.2

nginx version: nginx/1.10.3 (Ubuntu)

php-fpm version: php-fpm/5.6

php-fpm错误日志
WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
Nginx错误日志
eader from upstream, client: xxx.xxx.xxxx.xxx, server: xxx.domain.com, request: "GET /index.php/1/2/test.html HTTP/1.0", upstream: "fastcgi://unix:/run/php/php5.6-fpm.sock:", host: "xxx.domain.com", referrer: "http://xxx.domain.com./index.php/1/2/test.html?filter_limit=100"

从Nginx日志里面很容易看出来,是php-fpm出了问题,重启php-fpm后网站恢复正常。php-fpm日志里面给出了详细的错误提示,子进程数已经达到设置的最大值。

解决方案
/etc/php/5.6/fpm/php-fpm.conf
/etc/php/5.6/fpm/pool.d/www.conf

定位至配置文件,查找pm.max_children项 (5.6版本在www.conf中)

...
 pm = dynamic #对于专用服务器,pm可以设置为static。
 #如何控制子进程,选项有static和dynamic。如果选择static,则由pm.max_children指定固定的子进程数。如果选择dynamic,则由下开 参数决定:
 pm.max_children #,子进程最大数
 pm.start_servers #,启动时的进程数
 pm.min_spare_servers #,保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
 pm.max_spare_servers #,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
...

pm:表示使用那种方式,有两个值可以选择,就是static(静态)或者dynamic(动态)。

在更老一些的版本中,dynamic被称作apache-like。这个要注意看配置文件的说明。

下面4个参数的意思分别为:

pm.max_children:静态方式下开启的php-fpm进程数量
pm.start_servers:动态方式下的起始php-fpm进程数量
pm.min_spare_servers:动态方式下的最小php-fpm进程数
pm.max_spare_servers:动态方式下的最大php-fpm进程数量
区别

如果dm设置为 static,那么其实只有pm.max_children这个参数生效。系统会开启设置数量的php-fpm进程。

如果dm设置为 dynamic,那么pm.max_children参数失效,后面3个参数生效。

系统会在php-fpm运行开始 的时候启动pm.start_servers个php-fpm进程,然后根据系统的需求动态在pm.min_spare_servers和pm.max_spare_servers之间调整php-fpm进程数

关于数量的计算方法可以通过memory_get_peak_usage(true)这个函数获得内存峰值,以此作为单个请求的程序内存消耗消耗量,并考虑进php-fpm本身的基础内存消耗,可以得到一个近似的单进程内存消耗量。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK