【问题标题】:Getting 504 GATEWAY_TIMEOUT NodeJs获取 504 GATEWAY_TIMEOUT NodeJs
【发布时间】:2016-11-17 09:05:11
【问题描述】:

页面加载 60 秒后,我收到 504 GATEWAY_TIMEOUT http 响应。

它不是正在加载的实际页面,而是正在执行的进程。我预计它需要超过 60 秒,并且我尝试增加超时值,但它没有帮助。

我正在使用 express 框架进行路由,并将作业托管在 EB (AWS Elastic Beanstalk) 上。由于我增加了我可能在 AWS 控制台中的 EB 和负载均衡器上找到的所有超时值,我假设它必须是应用程序本身的超时设置为 60 秒。但是,我可能错了。

我的代码:

/* GET home page. */
router.get('/main',function(req, res, next) {
req.connection.setTimeout(600000);
        mainProcess(res);
        //res.send("mainProcess() called");
    });

更新:

除此之外,我还尝试了另一种方法。我将此代码添加到app.js

var connectTimeout = require('connect-timeout');
var longTimeout = connectTimeout({ time: 600000 });
app.use(longTimeout);

也没有用。

更新 2: 我也尝试过像这样增加/bin/www 的超时时间:

var server = http.createServer(app);
server.timeout=600000;

更新3: 我注意到超时与nginx配置有关。正如我的日志所说:upstream timed out (110: Connection timed out) while reading response header 但是,我想不出在 Elastic beanstalk 上编辑 nginx 配置的方法。我做了一些研究,但对我来说这一切似乎都不标准,而且对于这么简单的事情来说太死板了。

【问题讨论】:

  • 尝试在函数请求参数中使用 connect-timeout 包,如下所示:var timeout = require('connect-timeout'); app.post('/test', timeout('6s'), function(req, res, next) {console.log('test')})
  • 谢谢,但我看不到在哪里可以这样做,因为我在任何地方都没有app.post。 express 框架的做法不同。只有app.use('/', routes);
  • 这只是您的第一个代码中的一个示例,将其放在请求函数之前,如下所示:/* GET home page. */ var timeout = require('connect-timeout'); router.get('/main', timeout('6s'), function(req, res, next) { mainProcess(res); //res.send("mainProcess() called"); });
  • 试过了,还是一样的问题。
  • 你检查过请求是否命中 nodejs 吗?如果您可以访问您的 nginx 配置文件,例如:location / { proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_connect_timeout 60s; //Here is where you put the timeout }

标签: node.js amazon-web-services express


【解决方案1】:

根据您的 Update3 信息,我认为您应该配置您的 nginx 配置文件,例如:

server {
    listen  80;
    server_name     *.*;
    location / {
            proxy_pass http://192.168.0.100:8001;
            proxy_connect_timeout 60s;
            proxy_read_timeout 5400s;
            proxy_send_timeout 5400s;
            proxy_set_header host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_redirect default;
    }
}

proxy_read_timeout 和 proxy_send_timeout 和你的问题有关。

【讨论】:

  • 我花了 4 天时间调试才找到这块金子
【解决方案2】:

在您的 .ebextensions 配置文件中,添加以下代码:

container_commands:
  change_proxy_timeout:
    command: |
      sed -i '/\s*location \/ {/c \
              location / { \
                  proxy_connect_timeout       300;\
                  proxy_send_timeout          300;\
                  proxy_read_timeout          300;\
                  send_timeout                300;\
              ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf

【讨论】:

  • 这完全解决了我的问题。如果您要复制和粘贴以上内容(像我一样),则需要删除 container_commands 之前的多余空格
【解决方案3】:

通常当一个作业需要超过 30 秒或 40 秒时,我通常会通知正在执行的用户,然后我创建一个进程来处理数据库,而不是向服务器发出正常请求,以避免用户等待请求并避免该超时问题,例如,当您将服务器设置为侦听指定端口时,您可以尝试这样做:

//Create server with specified port
var server = app.listen(app.get('port'), function() {
  debug('Express server listening on port ' + server.address().port);
});
//set timeout time
server.timeout = 1000;

【讨论】:

  • 为尝试提供帮助而竖起大拇指
  • @OndrejTokar 我昨天做了一些测试,我也找不到解决方案,如果你找到了,请发布答案!
  • 感谢您投入时间。请查看我的最新更新。
【解决方案4】:

我遇到了同样的问题,所以我尝试将 timeout = 120000 设置如下:

var server= http.createServer(app).listen(port, function()
{
    console.log("Express server listening on port " + port)
})
server.timeout = 120000;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 2016-08-26
  • 1970-01-01
  • 2021-12-28
  • 2021-05-12
  • 1970-01-01
  • 2019-12-09
相关资源
最近更新 更多