欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Nginx的超时timeout配置详解

程序员文章站 2022-08-27 09:47:02
本文介绍 nginx 的 超时(timeout)配置。分享给大家,具体如下: nginx 处理的每个请求均有相应的超时设置。如果做好这些超时时间的限定,判定超时后资源被释...

本文介绍 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_timeout 60s;

client_body_timeout

指定客户端与服务端建立连接后发送 request body 的超时时间。如果客户端在指定时间内没有发送任何内容,nginx 返回 http 408(request timed out)。

# 配置段: http, server, location
client_body_timeout 20s;

client_header_timeout

客户端向服务端发送一个完整的 request header 的超时时间。如果客户端在指定时间内没有发送一个完整的 request header,nginx 返回 http 408(request timed out)。

# 配置段: http, server, location
client_header_timeout 10s;

send_timeout

服务端向客户端传输数据的超时时间。

# 配置段: http, server, location
send_timeout 30s;

客户度连接nginx超时, 建议5s内

接收客户端header超时, 默认60s, 如果60s内没有收到完整的http包头, 返回408

syntax: client_header_timeout time;
default:  
client_header_timeout 60s;
context:  http, server
defines a timeout for reading client request header. if a client does not transmit the entire header within this time, 
the 408 (request time-out) error is returned to the client.

接收客户端body超时, 默认60s, 如果连续的60s内没有收到客户端的1个字节, 返回408

syntax: client_body_timeout time;
default:  
client_body_timeout 60s;
context:  http, server, location
defines a timeout for reading client request body. the timeout is set only for a period between two successive read operations, not for the transmission of the whole request body. 
if a client does not transmit anything within this time, 
the 408 (request time-out) error is returned to the client.

keepalive时间,默认75s,通常keepalive_timeout应该比client_body_timeout大

syntax: keepalive_timeout timeout [header_timeout];
default:  
keepalive_timeout 75s;
context:  http, server, location
the first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. the zero value disables keep-alive client connections. 
the optional second parameter sets a value in the “keep-alive: timeout=time” response header field. two parameters may differ.

the “keep-alive: timeout=time” header field is recognized by mozilla and konqueror. msie closes keep-alive connections by itself in about 60 seconds.

可以理解为tcp连接关闭时的so_linger延时设置,默认5s

syntax: lingering_timeout time;
default:  
lingering_timeout 5s;
context:  http, server, location
when lingering_close is in effect, this directive specifies the maximum waiting time for more client data to arrive. if data are not received during this time, 
the connection is closed. otherwise, the data are read and ignored, and nginx starts waiting for more data again. 
the “wait-read-ignore” cycle is repeated, but no longer than specified by the lingering_time directive.

域名解析超时,默认30s

syntax: resolver_timeout time;
default:  
resolver_timeout 30s;
context:  http, server, location
sets a timeout for name resolution, for example:
resolver_timeout 5s;

发送数据至客户端超时, 默认60s, 如果连续的60s内客户端没有收到1个字节, 连接关闭

syntax: send_timeout time;
default:  
send_timeout 60s;
context:  http, server, location
sets a timeout for transmitting a response to the client. the timeout is set only between two successive write operations, 
not for the transmission of the whole response. if the client does not receive anything within this time, the connection is closed.

nginx与upstream server的连接超时时间

syntax: proxy_connect_timeout time;
default:  
proxy_connect_timeout 60s;
context:  http, server, location
defines a timeout for establishing a connection with a proxied server. it should be noted that this timeout cannot usually exceed 75 seconds.

nginx接收upstream server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭

syntax: proxy_read_timeout time;
default:  
proxy_read_timeout 60s;
context:  http, server, location
defines a timeout for reading a response from the proxied server. the timeout is set only between two successive read operations, 
not for the transmission of the whole response. if the proxied server does not transmit anything within this time, the connection is closed.

nginx发送数据至upstream server超时, 默认60s, 如果连续的60s内没有发送1个字节, 连接关闭

syntax: proxy_send_timeout time;
default:  
proxy_send_timeout 60s;
context:  http, server, location
sets a timeout for transmitting a request to the proxied server. the timeout is set only between two successive write operations, 
not for the transmission of the whole request. if the proxied server does not receive anything within this time, the connection is closed.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。