繁体   English   中英

Amazon Red Hat(非Ubuntu)中的随机“ 502错误错误网关”-Nginx + PHP-FPM

[英]Random “502 Error Bad Gateway” in Amazon Red Hat (Not Ubuntu) - Nginx + PHP-FPM

首先,我已经在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慢日志中,它显示:

[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存在一个错误,可能导致此错误。 但是我不认为Amazon实例(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;
          }
     }

我将从调整一些配置参数开始。

PHP-FPM

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

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

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

如果您使用的是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,以更好地利用keep live。


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

我在Ubuntu上遇到了这个问题,但是PHP-FPM上的request_terminate_timeoutfastcgi_send_timeout + fastcgi_read_timeout足以解决这个问题。

希望您能解决!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM