为什么我的 Amz EC2 服务器每天都会宕机几分钟?错误 503 和 502。这是一个 Rails 应用

Why my Amz EC2 server downs everyday for few minutes? Errors 503 and 502. It's a Rails app

不知道是哪个错误导致的问题。当我看到时,服务器已关闭并出现错误 503。在 Google Chrome 日志中,我有以下错误:

503 Service Unavailable: Back-end server is at capacity

当服务器关闭时,我无法通过 SSH 连接以查看错误日志。几分钟后服务器工作,我去 nginx 错误日志。

在日志中,我有常见的错误,例如:

ActiveRecord::RecordNotFound (Couldn't find Attachment with 'id'=4240)

我知道如何解决,我认为这个错误不是问题所在。

但是我也有这个错误:

Sending 502 response: application did not send a complete response
Process (pid=31880, group=/home/ubuntu/........./current/public) no longer exists! Detaching it from the pool. 

我认为是这个问题,但是我在网上查了一下,没有找到解决问题的原因和解决方法。

这个问题发生在我创建负载均衡器并使用 HTTPS 后。 以前,这个问题从来没有发生过。

关于我的服务器和应用程序:

Amazon Ec2 instance;
Using Classic Load Balancer (with Amazon Certificate Manager in https port);
Using Route 53;
Don't using Elastic IP;
OS: Ubuntu 14.04.2 LTS
ruby -v: 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
rails -v: Rails 4.2.3
nginx -v: nginx/1.8.0
passenger -v: Phusion Passenger version 5.0.10

负载均衡器健康检查设置如下:

Ping Target 
HTTP:80/index.html
Timeout 5 seconds
Interval    30 seconds
Unhealthy threshold 5
Healthy threshold   5

健康检查信息:

我在“负载平衡器监控”选项卡中打印了此内容。是 不健康的主机(计数)。为什么我的主机不健康?

解决方案

就我而言,问题出在资产预编译任务中。 我的应用程序中有很多资产,当我使用 capistrano 进行部署时,它会耗尽服务器。

另一方面,有时,资产是在部署后在页面加载期间预编译的。但是这个任务很慢,而且returns错误502、503和504。

它导致服务器停机,因为 CPU 利用率达到 100%,平均延迟也越来越高。

为了解决,我从 Capistrano 中删除了 assets precompile task。我在本地 PC 中预编译资产并将它们全部发送到 GIT 分支 MASTER。当我运行cap production deploy时,预编译任务不会运行。 .

中有更多详细信息

我对我的负载均衡器健康检查设置做了一些更改:

Ping Target HTTP:80/elb/index.html (I created in pubic folder this folder and file)
Timeout 5 seconds
Interval    30 seconds
Unhealthy threshold 2
Healthy threshold   10

Idle timeout: 65 seconds (equal my nginx timeout)

有了这个,我希望服务器上的任务 assets precompile 永远不会超过 运行。