我的 NodeJS 应用程序无缘无故地显示 502 错误

Gab*_*loo 1 ubuntu nginx node.js pm2 http-status-code-502

我的 digitalocean Droplet 有一个很大的问题,你能帮帮我吗?

我有一个 nodeJS 应用程序,它可以与 MySQL 一起使用,我使用 Nginx + Ubuntu

但无缘无故,我得到了 502 bad gateway

我升级了 Droplet,三天后我的应用程序再次关闭

但在我的 digitalocean 平台上真正奇怪的是 CPU % 和内存都很好

现在我已经将我的 Droplet 升级到了最后一个包

你能帮我吗

当我执行这个命令时sudo tail -30 /var/log/nginx/error.log

我收到这个错误

[error] 989#989: *6611 connect() failed (111: Connection refused) while connecting to upstream, client:
Run Code Online (Sandbox Code Playgroud)

我想指出的是,我的应用程序已经运行良好超过一年了

这是 nginx 代码

server {

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name domain.app;

        location / {
                proxy_pass http://localhost:3001;
                proxy_http_version 1.1;
                proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.app/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.app/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {
    if ($host = domain.app) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



        server_name domain.app;
    listen 80;
    return 404; # managed by Certbot


}
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助

小智 5

我在使用 NodeJS (express) 和 Application Load Balancer (AWS) 时也遇到了类似的问题。问题出在保持活动超时配置中,NodeJS 应用程序默认超时设置为比负载均衡器上配置的超时更短的时间,这导致服务器在请求(由负载均衡器发出)中间断开连接)。

一个简单的解决方法是将 的值设置server.keepAliveTimeout为高于负载均衡器上配置的值。这是一篇很棒的文章,对我有帮助,它涉及很多细节

nginx 默认保持活动时间为75 秒;NodeJS 的默认保持活动时间是5 秒。