Nginx php-fpm 502 bad gateway解决方法

共1491个字

 环境:

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本身的基础内存消耗,可以得到一个近似的单进程内存消耗量。

 

❤ 喜欢 8059

6条回应:“Nginx php-fpm 502 bad gateway解决方法”

  1. Nellie说道:

    Wow, marvelous blog format! How long have you ever been running a blog for?
    you make blogging glance easy. The total look
    of your web site is wonderful, as smartly as the content material!

    dysfunction [Nellie]

  2. Kamagra Kopen说道:

    An impressive share! I have just forwarded this onto a colleague who was conducting
    a little homework on this. And he in fact bought me lunch
    due to the fact that I stumbled upon it for him…
    lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending the time to discuss this issue here on your web page.

    Kamagra Kopen
    [url=http://kamagrainus.com/]Kamagra pills[/url]

  3. It’s an awesome post in support of all the internet viewers;
    they will obtain benefit from it I am sure.

  4. You really make it seem so easy with your presentation but I find
    this matter to be actually something which I think I would never understand.

    It seems too complex and extremely broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!

  5. Сialis说道:

    You really make it seem really easy along with your presentation however I find
    this matter to be really something which I believe I’d by no means
    understand. It sort of feels too complicated and extremely
    huge for me. I’m looking forward on your subsequent submit, I’ll try to get the
    hold of it!

发表评论

电子邮件地址不会被公开。

归档于 Ubuntu