【问题标题】:AWS ElasticBeanstalk NodeJS - 502 error: recv() failed (104: Connection reset by peer) while reading response header from upstreamAWS ElasticBeanstalk NodeJS - 502 错误:recv() 在从上游读取响应标头时失败(104:对等方重置连接)
【发布时间】:2020-06-27 07:07:04
【问题描述】:

我们计划将我们的 NodeJS 平台从普通的 EC2 迁移到 ElasticBeanstalk。在这些过程中,经过一番努力,我们已经部署了我们的应用程序并且能够访问和执行操作。但是,对于某些请求,我们收到 502 错误

检查我们在下面找到的日志后;


/var/log/nginx/error.log

2020/03/16 06:12:09 [错误] 3009#0: *119488 recv() 在从上游读取响应标头时失败(104:对等方重置连接),客户端:xxx.xx.xx.xxx ,服务器:,请求:“POST /www_auth/register HTTP/1.1”,上游:“http://127.0.0.1:8081/register”,主机:“****.us-east-2.elasticbeanstalk.com”

它是随机发生的,我没有任何线索。我觉得我错过/需要使用 nginx 添加的一些配置级别更改。

如果您有任何解决此问题的步骤/建议,不胜感激!

【问题讨论】:

    标签: node.js nginx amazon-elastic-beanstalk


    【解决方案1】:

    AWS Elastic Load Balancer 预连接到后端服务器,它可能会导致 ELB 认为连接已打开,但 Node.js 后端已经关闭它的竞争条件,因为 5 秒的 server.keepAliveTimeout idle inactivity,Node.js 中的默认值8.x 及更高版本。

    禁用 server.keepAliveTimeoutserver.headersTimeout 以解决此问题,或将这些超时设置为大于 AWS ELB 的空闲超时值的 ms 值。

    const app = express();
    
    // Set up the app...
    const server = app.listen(8080);
    
    // Disable both timeouts
    server.keepAliveTimeout = 0;
    server.headersTimeout = 0;
    

    此解决方案的功劳归于 Shuhei Kagawa:

    https://shuheikagawa.com/blog/2019/04/25/keep-alive-timeout/

    【讨论】:

    • 很好的答案!它似乎也适用于其他后端服务器!
    猜你喜欢
    • 1970-01-01
    • 2018-02-21
    • 2021-05-09
    • 1970-01-01
    • 2019-05-04
    • 2014-11-01
    • 2013-08-25
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多