Nginx的超時timeout配置詳解

架構師之路發表於2018-08-07

本文介紹 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 30 s;

客戶度連線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.

相關文章