Elastic Beanstalk 返回 “504 网关超时”。

huangapple go评论76阅读模式
英文:

Elasticbeanstalk returns "504 Gateway Timeout"

问题

我在我的API中有一个需要一些时间才能返回响应的端点(>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>

我该如何修复这个问题?

英文:

I have an endpoint in my API which takes some time to return a response (>1 min).

I have deployed my API to Elasticbeanstalk and now when I try to access it I get a 504 Gateway Timeout from Nginx

&lt;html&gt;
&lt;head&gt;&lt;title&gt;504 Gateway Time-out&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;&lt;h1&gt;504 Gateway Time-out&lt;/h1&gt;&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;

How can I fix that?

答案1

得分: 4

超时错误应该通过改进软件本身来解决,但如果由于任何原因无法实现改进,则可以增加nginx和负载均衡器的超时时间。

在之前的Amazon Linux版本中,您需要在名为.ebextensions的目录中部署带有自定义nginx配置的代码。

在Amazon Linux 2中,情况基本相同,只是有一个小的区别,您需要使用.platform文件夹来存放平台配置,而不是使用.ebextensions

因此,在您的应用程序的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

请参阅经典负载均衡器与应用程序负载均衡器配置。

在AWS文档中(截至2021年8月29日),较新的应用程序负载均衡器没有默认超时时间。经典负载均衡器的超时时间为60秒。

aws:elb:policies

aws:elbv2:loadbalancer

了解更多信息,请参阅:

英文:

Timeout errors such as yours should ideally fixed by improvement to the software itself, but if it cannot be done for any reason, then you can increase the timeout of your nginx and Load Balancer.

In previous versions of Amazon Linux you would need to deploy your code with custom nginx configuration inside a directory named .ebextensions

With Amazon Linux 2 things are quite the same with a slight difference, instead of using the .ebextensions you need to use the .platform folder for your platform's configurations.

So, inside your app's intended ElasticBeanstalk package create the following structure -

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

And add the following content to your timeout.conf file

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

You should be aware that in some cases you'll need to increase your Load Balancer's timeout by either manually configuring it with the AWS Console (Under EC2) or by providing a configuration file inside the .ebextensions directory

For example (Note: this configuration will vary by the type of Load Balancer which you use):

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

See Classic Load Balancer vs Application Load Balancer configuration

In AWS's docs (per 08/29/2021) the newer Application Load Balancer has no default timeout.
The Classic Load Balancer has a timeout of 60 seconds.

aws:elb:policies
vs
aws:elbv2:loadbalancer

For more info, see

huangapple
  • 本文由 发表于 2021年8月30日 00:31:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/68974919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定