Elasticbeanstalk returns“504 网关超时”

Elasticbeanstalk returns "504 Gateway Timeout"

我的 API 中有一个端点需要一些时间才能 return 响应(>1 分钟)。

我已经将我的 API 部署到 Elasticbeanstalk,现在当我尝试访问它时,我从 Nginx 收到 504 网关超时

<html>
<head><title>504 Gateway Time-out</title></head>
<body>
<center><h1>504 Gateway Time-out</h1></center>
</body>
</html>

我该如何解决?

理想情况下,像您这样的超时错误应该通过改进软件本身来解决,但如果由于任何原因无法解决,那么您可以增加 nginx 和负载均衡器的超时时间。

在以前版本的 Amazon Linux 中,您需要使用自定义 nginx 配置在名为 .ebextensions

的目录中部署代码

与亚马逊 Linux 2 点完全相同,但略有不同,而不是使用 .ebextensions,您需要使用 .platform 文件夹进行平台配置。

因此,在您的应用程序预期的 ElasticBeanstalk 包中创建以下结构 -

eb-package
└── src
└── .ebextensions
└── .platform
    └── nginx
        └── conf.d
            └── timeout.conf

并将以下内容添加到您的 timeout.conf 文件

proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;

您应该知道,在某些情况下,您需要通过使用 AWS 控制台(在 EC2 下)手动配置负载均衡器或在 .ebextensions 目录中提供配置文件来增加负载均衡器的超时时间

例如(注意:此配置会因您使用的负载均衡器类型而异):

option_settings:
   - namespace: aws:elb:policies
     option_name: ConnectionSettingIdleTimeout
     value: 300

查看 Classic Load Balancer 与 Application Load Balancer 配置

在 AWS 的文档中(每 08/29/2021),较新的 Application Load Balancer 没有默认超时。 Classic Load Balancer 的超时为 60 秒。

aws:elb:policies 对比 aws:elbv2:loadbalancer

有关详细信息,请参阅