我将Puma作为上游应用服务器运行,Riak作为后台数据库集群运行。当我发送一个请求,映射减少了大约25K用户的数据块,并从Riak返回到应用程序,我在Nginx日志中得到一个错误:
upstream读取时超时(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错误日志,找出哪个上游正在变慢
文件并相应地调整读取超时时间
在我的情况下,它是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;
...
}
参见:原文
首先,通过查看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;
...
}
参见:原文
你应该总是避免增加超时时间,我怀疑你的后端服务器的响应时间在任何情况下都是问题。
我通过清除连接keep-alive标志并根据这里的答案指定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;
}
}
不幸的是,我无法解释为什么这是有效的,也没有设法从链接中提到的文档中破译它,所以如果有人有一个解释,我很有兴趣听到它。