随机" 502错误网关"在亚马逊红帽(不是Ubuntu) - Nginx + PHP-FPM

时间:2015-03-23 00:51:55

标签: wordpress nginx varnish php

首先,我已经在Stackoverflow中搜索了502错误。有很多线程,但这次的不同之处在于错误出现时没有模式而且它不在Ubuntu中。

一切都很完美,但我的网站每周大约一次显示: 502 Bad Gateway 。 在第一次出错后,每个连接都会开始显示此消息。重启MySQL + PHP-FPM + Nginx + Varnish不起作用。

我必须克隆这个实例,再制作另一个实例,以便重新启动我的网站(它在Amazon EC2中托管)。

Nginx日志中,它会一次又一次地显示这些行:

[error] 16773#0: *7034 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1

MySQL或Varnish日志中没有任何内容。但是在 PHP-FPM 中,它显示了这些类型的行:

WARNING: [pool www] child 18978, script '/var/www/mysite.com/index.php' (request: "GET /index.php") executing too slow (10.303579 sec), logging
WARNING: [pool www] child 18978, script '/var/www/mysite.com/index.php' (request: "GET /index.php") execution timed out (16.971086 sec), terminating

PHP-FPM slowlog 内显示:

[pool www] pid 20401
script_filename = /var/www/mysite.com/index.php
w3_require_once() /var/www/mysite.com/wp-content/plugins/w3-total-cache/inc/define.php:1478

(在第1478行的文件“define.php”中,它有这行代码: require_once $ path;

我认为问题出在W3 Total Cache插件上。所以我删除了W3 Total Cache。 大约5天后,在 PHP-FPM慢日志

中再次出现此错误
script_filename = /var/www/mysite.com/index.php
wpcf7_load_modules() /var/www/mysite.com/wp-content/plugins/contact-form-7/includes/functions.php:283

(在第283行的文件“functions.php”中,它有这行代码: include_once $ file;

前几天,第一个错误发生在另一个部分:

script_filename = /var/www/mysite.com/wp-cron.php
curl_exec() /var/www/mysite.com/wp-includes/class-http.php:1510

又是代码的不同部分:

[pool www] pid 20509
script_filename = /var/www/mysite.com/index.php
mysql_query() /var/www/mysite.com/wp-includes/wp-db.php:1655

CPU,RAM ......发生此错误时一切都很稳定(使用率低于20%)。

我尝试了一切,但没有任何效果:

  • 移至更好的服务器(CPU和RAM)
  • 从Nginx,PHP-FPM,MySQL减少超时(我的页面加载速度快,所以我减少超时以杀死任何离群值进程)
  • 更改了PHP-FPM备用服务器的数量
  • 从Nginx和PHP-FPM改变了很多配置
  • 我知道PHP-FPM和Ubuntu存在一个可能导致此错误的错误。但我不认为亚马逊实例存在错误(Red Hat)。 (而且我不想从PHP-FPM迁移到Socks,因为我已经读过Socks在重载下运行不正常)

自5个月前以来每周都会发生这种情况。我很绝望。 我甚至将Nginx和PHP-FPM放在Linux的crontab中,以便每天重启这些服务。但它也没有用。

任何人都有任何建议可以解决这个问题吗?什么都有帮助!!

服务器

 Amazon c3.large (2 core and 3.75GB RAM)
 Linux Amazon Red Hat 4.8.2 64bits

PHP-FPM:

 listen = 127.0.0.1:9000
 listen.allowed_clients = 127.0.0.1
 listen.mode = 0664
 pm = ondemand
 pm.max_children = 480
 pm.start_servers = 140
 pm.min_spare_servers =140
 pm.max_spare_servers = 250
 pm.max_requests = 50
 request_terminate_timeout = 15s
 request_slowlog_timeout = 10s
 php_admin_flag[log_errors] = on

Nginx的:

  worker_processes  2;
     events {
          worker_connections  2048;
          multi_accept        on;
          use                 epoll;
     }
     http {
          include       /etc/nginx/mime.types;
          default_type  application/octet-stream;
          access_log  off;
          sendfile        on;
          tcp_nopush     on;
          tcp_nodelay    on;
          types_hash_max_size 2048;
          server_tokens off;
          client_max_body_size 8m;
          reset_timedout_connection on;
          index index.php index.html index.htm;
          keepalive_timeout  1;
          proxy_connect_timeout  30s;
          proxy_send_timeout  30s;
          proxy_read_timeout  30s;
          fastcgi_send_timeout 30s;
          fastcgi_read_timeout 30s;
          listen 127.0.0.1:8080;
          location ~ .php$ {
                    try_files $uri =404;
                    include fastcgi_params;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    fastcgi_keep_conn on;
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_param HTTP_HOST $host;
          }
     }

1 个答案:

答案 0 :(得分:0)

我首先要调整一些配置参数。

PHP-FPM

我认为你的pm值有些偏差,比我通常看到的在服务器上配置你的规格稍微高一点......但是你说内存消耗是正常的,所以这有点奇怪。

无论如何......对于pm.max_children = 480,考虑到默认情况下WordPress会将内存限制增加到40MB,最终会使用多达18 GB的内存,因此您肯定希望降低内存。

查看此帖子的第四部分,了解有关该帖子的更多信息:http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips-and-tricks/

如果你正在使用......比如nginx,MySQL,Varnish和其他服务的512MB,那么你的php-fpm大约需要3328 MB ...每个进程除以40 MB,pm.max_children应该大概是80 ......但即使是80,也是非常高的。

您可能还可以降低pm.start_serverspm.min_spare_serverspm.max_spare_servers的值。我更喜欢把它们保持在低水平,只是增加它们是必要的

对于pm.max_requests,您应该保留默认值500以避免服务器重生。我认为如果您怀疑内存泄漏,最好降低它。

Nginx的

keepalive_timeout更改为60以更好地利用保持活力。


除此之外,我认为一切看起来都很正常。

我在Ubuntu上遇到过这个问题,但PHP-FPM上的request_terminate_timeoutfastcgi_send_timeout + fastcgi_read_timeout足以摆脱它。

我希望你能解决它!