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

问题描述 投票:87回答:10

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

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

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

一旦放入代理,就会发生Nginx超时。

**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 timeout puma
10个回答
25
投票

您应该总是避免增加超时,我怀疑您的后端服务器响应时间在任何情况下都是问题。

我通过清除连接保持活动标志并根据答案指定http版本解决了这个问题:https://stackoverflow.com/a/36589120/479632

server {
    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;

        # these two lines here
        proxy_http_version 1.1;
        proxy_set_header Connection "";

        proxy_pass http://localhost:5000;
    }
}

不幸的是,我无法解释为什么这有效,并且无法从答案中提到的文档中解读它,所以如果有人有解释我会非常有兴趣听到它。


0
投票

希望它可以帮助某人:我遇到了这个错误,原因是phpfpm的日志文件夹上的错误权限,更改后因此phpfpm可以写入它,一切都很好。


23
投票

发生这种情况是因为您的上游需要太多才能回答请求,NGINX认为上游已经在处理请求时失败,因此它会响应错误。只需在location中包含并增加proxy_read_timeout即可。同样的事情发生在我身上,我在工作时使用内部应用程序超时1小时:

proxy_read_timeout 3600;

有了这个,NGINX将等待一个小时让它的上游返回一些东西。


15
投票

首先通过查询nginx错误日志文件并根据我的情况相应地调整读取超时来确定哪个上游正在减速它是fastCGI

2017/09/27 13:34:03 [error] 16559#16559: *14381 upstream timed out (110: Connection timed out) while reading response header from upstream, client:xxxxxxxxxxxxxxxxxxxxxxxxx", upstream: "fastcgi://unix:/var/run/php/php5.6-fpm.sock", host: "xxxxxxxxxxxxxxx", referrer: "xxxxxxxxxxxxxxxxxxxx"

所以我必须在我的服务器配置中调整fastcgi_read_timeout

 location ~ \.php$ {
     fastcgi_read_timeout 240;
     ...
 }

见:original post


9
投票

我认为这个错误可能由于各种原因而发生,但它可能特定于您正在使用的模块。例如,我使用uwsgi模块看到了这一点,因此必须设置“uwsgi_read_timeout”。


8
投票

在您的情况下,它有助于代理中的一点优化,或者您可以使用“#time out settings”

location / 
{        

  # time out settings
  proxy_connect_timeout 159s;
  proxy_send_timeout   600;
  proxy_read_timeout   600;
  proxy_buffer_size    64k;
  proxy_buffers     16 32k;
  proxy_busy_buffers_size 64k;
  proxy_temp_file_write_size 64k;
  proxy_pass_header Set-Cookie;
  proxy_redirect     off;
  proxy_hide_header  Vary;
  proxy_set_header   Accept-Encoding '';
  proxy_ignore_headers Cache-Control Expires;
  proxy_set_header   Referer $http_referer;
  proxy_set_header   Host   $host;
  proxy_set_header   Cookie $http_cookie;
  proxy_set_header   X-Real-IP  $remote_addr;
  proxy_set_header X-Forwarded-Host $host;
  proxy_set_header X-Forwarded-Server $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

7
投票

我建议查看error_logs,特别是在上游部分,它显示了超时的特定上游。

然后基于此,您可以调整proxy_read_timeout fastcgi_read_timeout或uwsgi_read_timeout。

还要确保您的配置已加载。

更多细节在这里Nginx upstream timed out (why and how to fix)


1
投票

正如许多其他人在此指出的那样,增加NGINX的超时设置可以解决您的问题。

但是,增加超时设置可能不像许多这些答案所暗示的那样简单。我自己也遇到了这个问题,并尝试在/etc/nginx/nginx.conf文件中更改我的超时设置,因为这些线程中的几乎每个人都建议。这对我来说没什么帮助; NGINX的超时设置没有明显变化。现在,几个小时后,我终于设法解决了这个问题。

解决方案在于this forum thread,它说的是你应该将你的超时设置放在/etc/nginx/conf.d/timeout.conf中(如果这个文件不存在,你应该创建它)。我使用了线程中建议的相同设置:

proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;

0
投票

从我们这边使用spdy与代理缓存。当缓存过期时,我们会收到此错误,直到缓存更新为止。


0
投票

我有同样的问题,导致这是rails控制器中的“每天”错误。我不知道为什么,但在生产中,puma一次又一次地运行错误导致消息:

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

可能是因为Nginx试图一次又一次地从puma中获取数据。有趣的是,即使我在控制器中调用了不同的操作,错误也会导致超时消息,因此,单个拼写错误会阻止所有应用程序。

检查您的log / puma.stderr.log文件以查看是否是这种情况。

© www.soinside.com 2019 - 2024. All rights reserved.