AWS弹性Beanstalk Golang服务器 – 504网关超时

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

aws elastic beanstalk golang server - 504 Gateway Time-out

问题

我在AWS弹性Beanstalk上运行着一个使用Go 1的Web服务器,操作系统是64位Amazon Linux 2。当我向服务器发出请求并执行多个进程时,会出现错误,整个过程需要大约90秒才能完成,所以在60秒后,AWS Beanstalk服务器会终止客户端连接并返回以下错误,尽管整个过程在90秒后完全结束。

错误信息如下:

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

应用程序的源文件结构如下:

.
├── application.go
├── cron.yaml
└── public
    └── index.html

当我在本地机器上进行测试时,一切正常,整个过程需要90秒,只有在Beanstalk上运行时才会出现此问题。我该如何解决这个问题?

英文:

I have a golang web server running on AWS elastic beanstalk in this machine: Go 1 running on 64bit Amazon Linux 2;
The error occurs when I make a request to the server and it executes several processes, so it takes around 90 seconds to complete the entire process, so after 60 seconds the AWS beanstalk server ends the client connection and returns the following error, although the process ends completely after 90 seconds.
It sends the following error:

&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;

Source application files:

.
├── application.go
├── cron.yaml
└── public
    └── index.html

When I tested it on my local machine It works well and It take 90 seconds, only when It runs on Beanstalk has this issue.
How can I fix it ??

答案1

得分: 1

504 Gateway Timeout表示nginx代理从上游应用程序等待响应的时间太长。如果这在通常几秒钟后返回的端点上发生,很可能是nginx代理正在尝试访问您的应用程序未监听的端口(或应用程序已崩溃)。

默认情况下,Elastic Beanstalk配置nginx代理将请求转发到应用程序的5000端口。您可以通过将PORT环境属性设置为您的主应用程序侦听的端口来覆盖默认端口。更多信息:AWS反向代理文档

确保您的应用程序代码侦听正确的端口。

http.ListenAndServe(":5000", nil);

另一个原因可能是应用程序代码崩溃。检查Elastic Beanstalk中的最后100行日志。您还可以通过SSH登录到服务器并运行cat /var/log/eb-engine.log来检索日志。

AWS弹性Beanstalk Golang服务器 – 504网关超时

反向代理

另外,看起来您正在尝试静态托管/public/index.html。您可以通过两种不同的方式来实现。

  1. 使用Go(不推荐)
http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":3000", nil)
  1. 使用Nginx(推荐)

通过扩展您的nginx配置,您可以使用代理服务器静态托管文件并在到达服务器应用程序之前重定向路由。

~/workspace/my-app/
|-- .platform
|   `-- nginx
|       `-- conf.d
|           `-- myconf.conf
英文:

504 Gateway Timeout indicates that the nginx proxy is waiting too long for a response from the upstream app. If this happens for an endpoint that usually returns after a few seconds, it is very likely that the nginx proxy is trying to reach a port that your app is not listening on (or that the app has crashed).

By default, Elastic Beanstalk configures the nginx proxy to forward requests to your application on port 5000. You can override the default port by setting the PORT environment property to the port on which your main application listens. More info: AWS Reverse Proxy Docs

Make sure that your application code listens on the right port.

http.ListenAndServe(&quot;:5000&quot;, nil);

Another reason for this could be a crash in the app code. Check the last 100 log lines in Elastic Beanstalk. You can also retrieve the logs by SSH'ing into your server and running cat /var/log/eb-engine.log.

AWS弹性Beanstalk Golang服务器 – 504网关超时

Reverse Proxy

On a separate note, it looks like you're trying to statically host /public/index.html. You can do this in 2 separate ways.

  1. Using Go (not recommended)
http.Handle(&quot;/&quot;, http.FileServer(http.Dir(&quot;./static&quot;)))
http.ListenAndServe(&quot;:3000&quot;, nil)
  1. Using Nginx (recommended)

By extending your nginx config you can use the proxy server to statically host files and redirect routes before they hit your server application.

~/workspace/my-app/
|-- .platform
|   `-- nginx
|       `-- conf.d
|           `-- myconf.conf

huangapple
  • 本文由 发表于 2022年9月4日 02:37:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/73594627.html
匿名

发表评论

匿名网友

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

确定