Nginx的超时timeout配置详解
本文介绍Nginx的超时(timeout)配置。分享给大家,具体如下:
Nginx处理的每个请求均有相应的超时设置。如果做好这些超时时间的限定,判定超时后资源被释放,用来处理其他的请求,以此提升Nginx的性能。
keepalive_timeout
HTTP是一种无状态协议,客户端向服务器发送一个TCP请求,服务端响应完毕后断开连接。
如果客户端向服务器发送多个请求,每个请求都要建立各自独立的连接以传输数据。
HTTP有一个KeepAlive模式,它告诉webserver在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。
KeepAlive在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。
Nginx使用keepalive_timeout来指定KeepAlive的超时时间(timeout)。指定每个TCP连接最多可以保持多长时间。Nginx的默认值是75秒,有些浏览器最多只保持60秒,所以可以设定为60秒。若将它设置为0,就禁止了keepalive连接。
#配置段:http,server,location keepalive_timeout60s;
client_body_timeout
指定客户端与服务端建立连接后发送requestbody的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx返回HTTP408(RequestTimedOut)。
#配置段:http,server,location client_body_timeout20s;
client_header_timeout
客户端向服务端发送一个完整的requestheader的超时时间。如果客户端在指定时间内没有发送一个完整的requestheader,Nginx返回HTTP408(RequestTimedOut)。
#配置段:http,server,location client_header_timeout10s;
send_timeout
服务端向客户端传输数据的超时时间。
#配置段:http,server,location send_timeout30s;
客户度连接nginx超时,建议5s内
接收客户端header超时,默认60s,如果60s内没有收到完整的http包头,返回408
Syntax:client_header_timeouttime; Default: client_header_timeout60s; Context:http,server Definesatimeoutforreadingclientrequestheader.Ifaclientdoesnottransmittheentireheaderwithinthistime, the408(RequestTime-out)errorisreturnedtotheclient.
接收客户端body超时,默认60s,如果连续的60s内没有收到客户端的1个字节,返回408
Syntax:client_body_timeouttime; Default: client_body_timeout60s; Context:http,server,location Definesatimeoutforreadingclientrequestbody.Thetimeoutissetonlyforaperiodbetweentwosuccessivereadoperations,notforthetransmissionofthewholerequestbody. Ifaclientdoesnottransmitanythingwithinthistime, the408(RequestTime-out)errorisreturnedtotheclient.
keepalive时间,默认75s,通常keepalive_timeout应该比client_body_timeout大
Syntax:keepalive_timeouttimeout[header_timeout]; Default: keepalive_timeout75s; Context:http,server,location Thefirstparametersetsatimeoutduringwhichakeep-aliveclientconnectionwillstayopenontheserverside.Thezerovaluedisableskeep-aliveclientconnections. Theoptionalsecondparametersetsavalueinthe“Keep-Alive:timeout=time”responseheaderfield.Twoparametersmaydiffer.
The“Keep-Alive:timeout=time”headerfieldisrecognizedbyMozillaandKonqueror.MSIEcloseskeep-aliveconnectionsbyitselfinabout60seconds.
可以理解为TCP连接关闭时的SO_LINGER延时设置,默认5s
Syntax:lingering_timeouttime; Default: lingering_timeout5s; Context:http,server,location Whenlingering_closeisineffect,thisdirectivespecifiesthemaximumwaitingtimeformoreclientdatatoarrive.Ifdataarenotreceivedduringthistime, theconnectionisclosed.Otherwise,thedataarereadandignored,andnginxstartswaitingformoredataagain. The“wait-read-ignore”cycleisrepeated,butnolongerthanspecifiedbythelingering_timedirective.
域名解析超时,默认30s
Syntax:resolver_timeouttime; Default: resolver_timeout30s; Context:http,server,location Setsatimeoutfornameresolution,forexample: resolver_timeout5s;
发送数据至客户端超时,默认60s,如果连续的60s内客户端没有收到1个字节,连接关闭
Syntax:send_timeouttime; Default: send_timeout60s; Context:http,server,location Setsatimeoutfortransmittingaresponsetotheclient.Thetimeoutissetonlybetweentwosuccessivewriteoperations, notforthetransmissionofthewholeresponse.Iftheclientdoesnotreceiveanythingwithinthistime,theconnectionisclosed.
nginx与upstreamserver的连接超时时间
Syntax:proxy_connect_timeouttime; Default: proxy_connect_timeout60s; Context:http,server,location Definesatimeoutforestablishingaconnectionwithaproxiedserver.Itshouldbenotedthatthistimeoutcannotusuallyexceed75seconds.
nginx接收upstreamserver数据超时,默认60s,如果连续的60s内没有收到1个字节,连接关闭
Syntax:proxy_read_timeouttime; Default: proxy_read_timeout60s; Context:http,server,location Definesatimeoutforreadingaresponsefromtheproxiedserver.Thetimeoutissetonlybetweentwosuccessivereadoperations, notforthetransmissionofthewholeresponse.Iftheproxiedserverdoesnottransmitanythingwithinthistime,theconnectionisclosed.
nginx发送数据至upstreamserver超时,默认60s,如果连续的60s内没有发送1个字节,连接关闭
Syntax:proxy_send_timeouttime; Default: proxy_send_timeout60s; Context:http,server,location Setsatimeoutfortransmittingarequesttotheproxiedserver.Thetimeoutissetonlybetweentwosuccessivewriteoperations, notforthetransmissionofthewholerequest.Iftheproxiedserverdoesnotreceiveanythingwithinthistime,theconnectionisclosed.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。