Node应用程序的NGINX 502错误网关中的HTTP请求超时为2分钟 [英] Http request timeout at 2 minutes in NGINX 502 bad gateway in Node app

查看:666
本文介绍了Node应用程序的NGINX 502错误网关中的HTTP请求超时为2分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个超时问题,希望能得到一些帮助.我有一个http请求,可能需要2.5分钟才能返回响应.我在Angular中有3分钟的超时处理,在NodeJS中也有3分钟的超时处理.我的nginx设置有200秒超时,而我的Elastic Load Balancing连接超时设置为4分钟.但是,我仍然在确切的2分钟看到502错误的网关nginx 1.4.6(Ubuntu)错误.我想错过更长的超时时间吗?

I’m been scratching my head on this timeout issue and hope to get some helps. I have a http request that might take 2.5 minutes to return the response. I have timeout handling in Angular for 3 minutes, and NodeJS for 3 minutes as well. My nginx setting have 200 seconds timeout and my Elastic Load Balancing Connection Timeout is set to 4 minutes. However, I keep seeing the 502 bad gateway nginx 1.4.6 (Ubuntu) error at exact 2 minutes. Is there any part that I miss to have longer timeout?

我的nginx设置:

server {
    listen 80;
    server_name;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log debug;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Connection "";
        proxy_http_version 1.1;
        proxy_pass http://localhost:8060;
        proxy_redirect off;
        proxy_connect_timeout 200s;
        proxy_send_timeout 200s;
        proxy_read_timeout 200s;
        send_timeout 200s;
    }
    #Handle protected assets using 'internal' directive documented here: https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/
    location /protected {
        internal;
        expires -1;
    }
}

我的NodeJS设置正在使用连接超时

My NodeJS setting is using connect-timeout

var timeout = require('connect-timeout');
app.use(timeout(300000));

推荐答案

我今天才碰到这个问题,并且可能找到了答案-节点的http模块中有120 s的硬编码超时.我必须在给定的请求处理程序中设置套接字超时,如下所示:

I just came across this today, and probably found an answer - there is 120 s hard coded timeout in node's http module. I had to set socket timeout in given request handler like this:

yourHandler(req, res) {
  req.socket.setTimeout(3600e3); // 1 hour

  // ... do the real work

  res.json(...);
}

您也可以将此限制设置为0以禁用此超时.

You can also set this limit to 0 to disable this timeout.

https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback

最初在这里找到了答案: https://forum. nginx.org/read.php?2,214230,214239#msg-214239

originally found the answer here: https://forum.nginx.org/read.php?2,214230,214239#msg-214239

这篇关于Node应用程序的NGINX 502错误网关中的HTTP请求超时为2分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆