简体   繁体   English

Node.js:1 分钟后 http 请求超时

[英]Node.js: http request timing out after 1 minute

I am using Node.js (version 0.10.28) to make an http.request to my Ruby API for a large amount of data (21,000 rows from a PostgreSQL database).我正在使用 Node.js(版本 0.10.28)向我的 Ruby API 发出http.request以获取大量数据(来自 PostgreSQL 数据库的 21,000 行)。 The problem is that the request seems to time out and return a 404 error after 1 minute.问题是请求似乎超时并在 1 分钟后返回 404 错误。 I know this is true as the Ruby API returns 61 seconds for the request, and I time how long the Node's request takes (results in 60 seconds and the 404).我知道这是真的,因为 Ruby API 为请求返回 61 秒,并且我计算节点的请求需要多长时间(结果为 60 秒和 404)。 However, if I wget or use jQuery's $.ajax with a timeout of 4 minutes, I can get my 21,000 rows.但是,如果我wget或使用 jQuery 的$.ajax超时为 4 分钟,我可以获得 21,000 行。 So clearly it can't be a 404, as other ways of getting the data work.很明显,它不能是 404,因为其他获取数据的方法是有效的。

I'm a little confused too because everywhere I look, the http.request isn't supposed to time out until after 2 minutes, according to:我也有点困惑,因为无论我看http.requesthttp.request都不应该在 2 分钟后超时,根据:

Node.js docs Node.js 文档

GitHub issue #1 GitHub 问题 #1

GitHub issue #2 GitHub 问题 #2

I have tried several things to get this working, including: setting express's middleware ;我尝试了几件事情来让它工作,包括:设置express 的中间件 listening for the socket timeout and resuming the request; 监听套接字超时并恢复请求; and setting the timeout to 0, so there's no timeout.并将超时设置为 0,所以没有超时。 Unfortunately, none of these ways worked, or at least from what I've understood.不幸的是,这些方法都不起作用,或者至少根据我的理解。

For some clarity, here's my code... setting that limit means return only 18,000 records, and that seems to be the cut off point for the API taking longer than 60 seconds:为了清楚起见,这是我的代码...设置限制意味着只返回 18,000 条记录,这似乎是 API 花费超过 60 秒的截止点:

var http = require('http');
var options = {
  path: '/api/records.json?limit=18000',
  method: 'GET',
  host: 'localhost',
  requestCert: true,
  rejectUnauthorized: false
};
var req = http.request(options, function(res) {
  var endDate = new Date();
  console.log('done', endDate - startDate);
  var output = [];
  res.on('data', function(chunk) {
    output.push(chunk);
  });
  res.on('end', function() {
    var data = output.join('');
    console.log(data);
    return {data: data, success: true};
  });
});
req.on('socket', function(socket) {
  socket.setTimeout(0);  // no timeout
  socket.on('timeout', function() {
    socket.resume();  // tried resuming the timeout
  });
});
req.end();
var startDate = new Date();
console.log('starting', startDate);

The API URL is valid and works, as I've verified that through wget and $.ajax , so how do I fix the timeout issue in Node? API URL 有效且有效,因为我已经通过wget$.ajax验证了这一点,那么如何解决 Node 中的超时问题?

UPDATE更新

Upon further inspection, if I take out socket.setTimeout(0);经过进一步检查,如果我取出socket.setTimeout(0); and try to get into the socket.on('timeout' , I don't actually get into the callback... which is odd, but it's always 60 seconds before I get the 404并尝试进入socket.on('timeout' ,我实际上并没有进入回调......这很奇怪,但在我得到 404 之前总是 60 秒

这是一个 nginx 问题,因为我使用它来代理到我的 API,因此通过设置proxy_read_timeout 180s对代理的配置进行了简单的更改。

Friends have had the same problem After working for 15 days, the source of the problem is "server.headersTimeout = 7200000;"朋友也遇到过同样的问题工作了15天后,问题的根源是“server.headersTimeout = 7200000;” you can fix it by adding this code您可以通过添加此代码来修复它

As you said it's a nginx problem, but probably if you don't set in the nginx config the proxy parameters nginx default depends on the operative system, because in Ubuntu 16.04 I haven't any problem with a 10min timeout and without any proxy parameter, but in centos and redhat I saw the 1 min timeout restriction.正如你所说,这是一个 nginx 问题,但可能如果你没有在 nginx 配置中设置代理参数 nginx 默认值取决于操作系统,因为在 Ubuntu 16.04 中我没有任何问题,10 分钟超时并且没有任何代理参数,但在 centos 和 redhat 中,我看到了 1 分钟超时限制。

I add a configuration example because there are two parameters more than proxy_read_timeout that you need to set:我添加了一个配置示例,因为需要设置的参数比proxy_read_timeout多了两个:

server {
    listen       80;
    server_name  myserver;

    location / {
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

        proxy_connect_timeout      180s;
        proxy_send_timeout         180s;
        proxy_read_timeout         180s;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}

For this issue the important parameters are:对于这个问题,重要的参数是:

proxy_connect_timeout      180s;
proxy_send_timeout         180s;
proxy_read_timeout         180s;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM