首页 > 解决方案 > EC2 节点后端应用程序 [504 网关超时]

问题描述

工作了几个小时,然后我在应用程序的后端收到 504 网关超时错误。

EC2 实例使用 nginx 和 PM2 运行 ubuntu。

/etc/nginx/sites-available.conf 文件:

server {
  listen 80;
  server_name mydomain.com;
  root /home/ubuntu/app;
  index index.html;

  access_log /var/log/nginx/app.access.log;
  error_log /var/log/nginx/app.error.log;
  location / {
    try_files $uri /index.html =404;
  }
}

server {
    listen 8080;
    server_name mydomain.com;
    location / {
      proxy_pass http://127.0.0.1:3000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_redirect off;
     }
}

我相信我的配置是正确的,因为它可以正常工作,但只能运行几个小时。然后我得到错误。PM2 实例仍然在线并运行。我认为这可能是节点应用程序由于某种原因崩溃了,但是如何在 ubuntu EC2 上解决这个问题?它在我的本地机器上完美运行。

任何建议,将不胜感激。

标签: nginxamazon-ec2pm2

解决方案


花了几个小时在这上面。事实证明 PM2 和 Nginx 并不总是能很好地协同工作,更改 Nginx 配置并重新启动 Nginx 和 pm2 最终为我解决了这个问题。

具体来说,添加这两行:

proxy_set_header Connection '';
keepalive_timeout 10;

推荐阅读