Skip to content

timeout

http {
client_header_timeout 10s;
 client_body_timeout 10s;
 proxy_connect_timeout 10s;
 proxy_send_timeout 55s;
 proxy_read_timeout 60s;
 keepalive_timeout  65s;
}
http {
client_header_timeout 10s;
 client_body_timeout 10s;
 proxy_connect_timeout 10s;
 proxy_send_timeout 55s;
 proxy_read_timeout 60s;
 keepalive_timeout  65s;
}
//nginx代理与后端服务器连接超时时间(代理连接超时)还未连接成功
Syntax:  proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location

//nginx代理等待后端服务器的响应时间(连接成功,等待读取资源)
Syntax:  proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location

//后端服务器数据回传给nginx代理超时时间(后段服务器给nginx传输资源的时间)
Syntax:  proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location
//nginx代理与后端服务器连接超时时间(代理连接超时)还未连接成功
Syntax:  proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location

//nginx代理等待后端服务器的响应时间(连接成功,等待读取资源)
Syntax:  proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location

//后端服务器数据回传给nginx代理超时时间(后段服务器给nginx传输资源的时间)
Syntax:  proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location

第一步请求头过来,

第二步连接upstream的server,

第三步将请求发送给upstream的server,

第四步接收upstream的server数据,

第五步是服务结束后是否采用长连接

  • client_header_timeout 10s

默认:60s

配在:http中、server中、location中

  • client_body_timeout 10s

默认:60s

配在:http中、server中

  都跟请求相关,就一起理解了说了,这两个参数是对请求头和请求体(想了解请求头和请求体的概念自己百度)的超时时间,就是从三次握手到第一次读取请求头和请求体失败的时间。比如当前服务器负载大、网络卡,恰好在第一次读取请求头或请求提时没有得到且时间超过10s了,tengine就会超时报错,对于我当前应用而言,60s显而是太长了,优化到10s
  • proxy_connect_timeout 10s;

默认:60s

配在:http中、server中、location中

   在收到请求头后,会将请求转发到upstream里面的server,这个呢就是与对应的server连接的超时时间,设置时最大值不能超过75s,我这里的server和tengine是放在同一个交换机上的内网,所以将连接时间优化到10s,超过10s连接不上,说明业务有问题了
  • proxy_send_timeout 55s

默认:60s

配在:http中、server中、location中

  在与upstream的server建立连接后,就会把请求往server发送,这个时间是两次数据的发送时间差,不是整个发送过程的。比如说负载大、网络卡,在tengine向server发送请求时突然卡了一下,然后继续发送,而这两次的时间差(其实就是两次write的时间差)超过了我设置的55s,tengine就会超时报错,对于这个参数,我当前优化的是55s
  • proxy_read_timeout 60s;

默认:60s

配在:http中、server中、location中

 在将请求发送给upstream的server后,后端server就会回传数据,这个时间是两次收取数据的时间差,不是整个的接收时间。比如说负载大、网络卡,在第1次收到请求的数据时断了,然后过了60s后才收到后面的数据,这两个时间差(其实就是两次read的时间差)超过了设置的60s,tengine(nginx)就会超时报错,我当前走的是默认设置60s
  • keepalive_timeout 65s

默认:75s

配在:http中、server中、location中

  http是无状态的协议,当服务结束后,就面临着是否断开tcp连接的问题,当客户端或者服务器端需要时,可以在建链的时候采用长连接方式,即服务结束后在一段时间内不断开连接,当再有请求过来时省掉了建链的资源消耗,超时后tengine(nginx)会主动断开连接,当然配置里还有另外一个参数 keepalive_requests 600;,这个参数是说即使长连接没到过期时间,但服务的http总数量超过指定值后也是要断开连接,我目前设置的是600
  • resolver_timeout 10s

默认:30s

配在:http中、server中、location中

这个是dns解析超时时间,如果用作正向代理时就有用了,同时可以用resolver 127.0.0.1 valid=10m;指令来指定dns,后面是解析后缓存的有效时间。

server 127.0.0.1:9999 max_fails=20 fail_timeout=10s;

这个是指某一个upstream的server如果失败20次后,不可以操作的时间,默认就是10s,其实可以另外的写法配在http中,我习惯直接配在server的后端。

  • keepalive_timeout 65 70

这是前端keepalive_timeout的一个延伸配置,前面65是告诉客户端我给你保持多久,后面一个是多久我就给断开连接了

502,no live

现象,*379803415 no live upstreams while connecting to upstream 的日志

no live upstreams

upstream prematurely closed connection while reading response header from upstream

tcpdump -nps0 -iany -w /tmp/20180617.pcap net [ip] and net [ip]

解决方式

1、如果系统并发量不大,没有必要开启长连接,有两种方式,

一、第一台nginx可以去除 proxy_http_version 1.1; proxy_set_header Connection "0";这两个配置;

二、第二台nginx的keepalive_timeout可以配置为0(默认是75)。

2、上述问题我的解决方案是:暂时调大keepalive_timeout的值,先观察,但很有可能还是会有这个问题。

错误日志类型

  • 类型1: upstream timed out
  • 类型2: connect() failed
  • 类型3: no live upstreams
  • 类型4: upstream prematurely closed connection
  • 类型5: 104: Connection reset by peer
  • 类型6: client intended to send too large body
  • 类型7: upstream sent no valid HTTP/1.0 header

详细说明

类型错误日志原因解决办法
类型错误日志原因解决办法
1upstream timed out (110: Connection timed out) while connecting to upstreamnginx与upstream建立tcp连接超时,nginx默认连接建立超时为200ms排查upstream是否能正常建立tcp连接
1upstream timed out (110: Connection timed out) while reading response header from upstreamnginx从upstream读取响应时超时,nginx默认的读超时为20s,读超时不是整体读的时间超时,而是指两次读操作之间的超时,整体读耗时有可能超过20s排查upstream响应请求为什么过于缓慢
2connect() failed (104: Connection reset by peer) while connecting to upstreamnginx与upstream建立tcp连接时被reset排查upstream是否能正常建立tcp连接
2connect() failed (111: Connection refused) while connecting to upstreamnginx与upstream建立tcp连接时被拒排查upstream是否能正常建立tcp连接
3no live upstreams while connecting to upstreamnginx向upstream转发请求时发现upstream状态全都为down排查nginx的upstream的健康检查为什么失败
4upstream prematurely closed connectionnginx在与upstream建立完tcp连接之后,试图发送请求或者读取响应时,连接被upstream强制关闭排查upstream程序是否异常,是否能正常处理http请求
5recv() failed (104: Connection reset by peer) while reading response header from upstreamnginx从upstream读取响应时连接被对方reset排查upstream应用已经tcp连接状态是否异常
6client intended to send too large body客户端试图发送过大的请求body,nginx默认最大允许的大小为1m,超过此大小,客户端会受到http 413错误码调整请求客户端的请求body大小;调大相关域名的nginx配置:client_max_body_size;
7upstream sent no valid HTTP/1.0 headernginx不能正常解析从upstream返回来的请求行排查upstream http响应异常