NGINX:从上游读取响应头时上游超时(110:连接超时)

问题描述:

我将 Puma 作为上游应用程序服务器运行,将 Riak 作为我的后台数据库集群运行.当我发送一个请求,为大约 25000 个用户映射减少大量数据并将其从 Riak 返回到应用程序时,我在 Nginx 日志中收到一个错误:

I have Puma running as the upstream app server and Riak as my background db cluster. When I send a request that map-reduces a chunk of data for about 25K users and returns it from Riak to the app, I get an error in the Nginx log:

读取时上游超时(110:连接超时)来自上游的响应头

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

如果我在没有 nginx 代理的情况下直接查询我的上游,使用相同的请求,我会得到所需的数据.

If I query my upstream directly without nginx proxy, with the same request, I get the required data.

一旦代理被放入,Nginx 就会超时.

The Nginx timeout occurs once the proxy is put in.

**nginx.conf**

http {
    keepalive_timeout 10m;
    proxy_connect_timeout  600s;
    proxy_send_timeout  600s;
    proxy_read_timeout  600s;
    fastcgi_send_timeout 600s;
    fastcgi_read_timeout 600s;
    include /etc/nginx/sites-enabled/*.conf;
}

**virtual host conf**

upstream ss_api {
  server 127.0.0.1:3000 max_fails=0  fail_timeout=600;
}

server {
  listen 81;
  server_name xxxxx.com; # change to match your URL

  location / {
    # match the name of upstream directive which is defined above
    proxy_pass http://ss_api; 
    proxy_set_header  Host $http_host;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_cache cloud;
    proxy_cache_valid  200 302  60m;
    proxy_cache_valid  404      1m;
    proxy_cache_bypass $http_authorization;
    proxy_cache_bypass http://ss_api/account/;
    add_header X-Cache-Status $upstream_cache_status;
  }
}

Nginx 有一堆超时指令.我不知道我是否遗漏了一些重要的东西.任何帮助将不胜感激....

Nginx has a bunch of timeout directives. I don't know if I'm missing something important. Any help would be highly appreciated....

出现这种情况是因为你的上游响应请求的时间太长,而 NGINX 认为上游已经处理请求失败,所以它响应一个错误.只需在 location 配置块中包含并增加 proxy_read_timeout.同样的事情发生在我身上,我在工作中使用了 1 小时的内部应用超时时间:

This happens because your upstream takes too long to answer the request and NGINX thinks the upstream already failed in processing the request, so it responds with an error. Just include and increase proxy_read_timeout in location config block. Same thing happened to me and I used 1 hour timeout for an internal app at work:

proxy_read_timeout 3600;

这样,NGINX 将等待一个小时(3600 秒)让上游返回一些东西.

With this, NGINX will wait for an hour (3600s) for its upstream to return something.