【问题标题】:Nginx upstream timed out for long unicorn requestNginx 上游因长独角兽请求而超时
【发布时间】:2015-05-09 15:59:24
【问题描述】:

我有一个由 nginx/unicorn 提供的 Rails 应用程序,它有一个可能需要 2-3 分钟的特定请求,因为它正在生成多个 PDF 并将它们添加到一个 zip 文件并使用 send_data 允许客户端下载一次报告多个报告。

最初我的独角兽工人在 30 秒后被杀死,所以我在 unicorn.rb 文件中增加了超时。现在请求在 60 秒而不是 30 秒后收到 504 错误。所以我的独角兽工人被杀了,但 nginx 正在超时。

这是我的 nginx error_log 中的错误消息:

upstream timed out (110: Connection timed out) while reading response header from upstream

我已尝试增加所有有意义的 nginx 超时设置,但在 service nginx restart 之后,请求仍然超时。

这是我的 nginx.conf 和启用站点/默认设置

/***nginx.conf***/
http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 300s;
  client_header_timeout 300s;
  client_body_timeout 300s;
  send_timeout 300s;
  types_hash_max_size 2048;
}



/***sites-enabled/default***/
upstream app {
  unix:[PATH]/unicorn.sock fail_timeout=120s;
}

server {

  listen 80;
  root [PATH];

  server_name www.[URL].com [URL].com;
  proxy_read_timeout 600s;

  try_files $uri/index.html $uri @app;

  access_log /var/log/nginx/APP_access.log combined;
  error_log /var/log/nginx/APP_error.log;

  location @app {
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app;
    proxy_read_timeout 600s;
    proxy_connect_timeout 600s;
    proxy_send_timeout 600s;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 75;
}

谁能告诉我为什么 nginx 在 60 秒后仍然超时?

【问题讨论】:

  • 您确定不需要再增加独角兽超时时间 - 该错误消息似乎表明“上游”超时存在问题
  • 真的,您应该使用后台作业来处理耗时超过 30 秒的事情 - 您可能需要查看延迟作业:github.com/collectiveidea/delayed_job
  • @house9,是的,我忘了提。需要后台作业,但它不在 MVP 中。当独角兽超时是问题时,错误消息出现在独角兽错误日志中,它说“超时(31s > 30s)”现在错误是上面的错误,它在 ngoni 日志中。我还将独角兽超时设置为超过 60 秒。

标签: ruby-on-rails nginx unicorn


【解决方案1】:

我可能已经通过重新启动服务器解决了这个问题。更改我的任何 nginx 文件后,我会运行 service nginx reloadservice nginx restart 但我想这实际上并没有重置我正在扩展的超时设置。

现在的问题是我不是 100% 我扩展的许多超时设置中的哪一个是我需要的。我可能会向后工作以解决这个问题,但我猜它是代理设置之一。

正确的解决方案仍然是(正如@house9 所说)实际设置后台作业,但正如我所说的那样,这是解决我的问题的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 2012-04-14
    • 2012-06-11
    • 2012-09-17
    • 2012-05-27
    • 2014-08-04
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多