关于nginx的超时的参数整理,便于整体的认知和理解nginx的机制流程。
proxy转发模块的超时设置:
参数01:proxy_connect_timeout 默认60s
# 跟后端服务器连接的超时时间—发起握手等候响应超时时间。这个不是等待后端返回页面的时间,那是由proxy_read_timeout声明的。如果你的upstream服务器起来了,但是hanging住了(例如,没有足够的线程处理请求,所以把你的请求放到请求池里稍后处理),那么这个声明是没有用的,因为与upstream服务器的连接已经建立了。
This directive assigns a timeout for the connection to the proxyserver. This is not the time until the server returns the pages, this is the proxy_read_timeout statement. If your proxyserver is up, but hanging (e.g. it does not have enough threads to process your request so it puts you in the pool of connections to deal with later), then this statement will not help as the connection to the server has been made. It is necessary to keep in mind that this time out cannot be more than 75 seconds.
参数02:proxy_read_timeout 默认60s
#连接成功后-等候后端服务器响应的时间-其实已经进入后端的排队之中等候处理
nginx代理某个请求时,这个参数决定nginx会等多久。这要求我们充分了解业务,比如哪些访问大概会耗时多久。如果超时后,nginx关闭连接。
This directive sets the read timeout for the response of the proxied server. It determines how long NGINX will wait to get the response to a request. The timeout is established not for entire response, but only between two operations of reading.
In contrast to proxy_connect_timeout, this timeout will catch a server that puts you in it's connection pool but does not respond to you with anything beyond that. Be careful though not to set this too low, as your proxy server might take a longer time to respond to requests on purpose (e.g. when serving you a report page that takes some time to compute). You are able though to have a different setting per location, which enables you to have a higher proxy_read_timeout for the report page's location.
If the proxied server nothing will communicate after this time, then nginx is shut connection.
参数03:proxy_send_timeout 默认60s
#后端服务器数据回传时间-就是在规定时间内后端服务器必须传完所有的数据。
这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接。
upstream 模块:
参数01 :max_fails = number 默认1,0表示关闭这项检查。
#在参数fail_timeout指定的时间内对后端服务器请求失败的次数。如果检测到后端服务器无法连接及发生服务器错误(404除外),则标记为失败。
参数02: fail_timeout=TIME
#在经历参数max_fails 设置的失败次数后,暂停的时间。注意,这段时间表示nginx认为服务是不可用的,后台会报no live upstream 的报错。
总结:通过上述几个参数,按顺序看,实际上就是nginx完整的处理流程。