Nginx的超時timeout配置詳解

keepalive_timeouthtml

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.

HTTP 是一種無狀態協議,客戶端向服務器發送一個 TCP 請求,服務端響應完畢後斷開鏈接。nginx

若是客戶端向服務器發送多個請求,每一個請求都要創建各自獨立的鏈接以傳輸數據。git

HTTP 有一個 KeepAlive 模式,它告訴 webserver 在處理完一個請求後保持這個 TCP 鏈接的打開狀態。若接收到來自客戶端的其它請求,服務端會利用這個未被關閉的鏈接,而不須要再創建一個鏈接。github

KeepAlive 在一段時間內保持打開狀態,它們會在這段時間內佔用資源。佔用過多就會影響性能。web

Nginx 使用 keepalive_timeout 來指定 KeepAlive 的超時時間(timeout)。指定每一個 TCP 鏈接最多能夠保持多長時間。Nginx 的默認值是 75 秒,有些瀏覽器最多隻保持 60 秒,因此能夠設定爲 60 秒。若將它設置爲 0,就禁止了 keepalive 鏈接。後端

keepalive時間,默認75s,一般keepalive_timeout應該比client_body_timeout大瀏覽器

The 「Keep-Alive: timeout=time」 header field is recognized by Mozilla and Konqueror. MSIE closes keep-alive connections by itself in about 60 seconds.服務器

client_header_timeoutide

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.

接收客戶端header超時, 默認60s, 若是60s內沒有收到完整的http包頭, 返回408post

客戶端向服務端發送一個完整的 request header 的超時時間。若是客戶端在指定時間內沒有發送一個完整的 request header,Nginx 返回 HTTP 408(Request Timed Out)。

client_body_timeout

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.

指定客戶端與服務端創建鏈接後發送 request body 的超時時間。若是客戶端在指定時間內沒有發送任何內容,Nginx 返回 HTTP 408(Request Timed Out)。

接收客戶端body超時, 默認60s, 若是連續的60s內沒有收到客戶端的1個字節, 返回408

send_timeout

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.

服務端向客戶端傳輸數據的超時時間。默認60s, 若是連續的60s內客戶端沒有收到1個字節, 鏈接關閉

客戶度鏈接nginx超時, 建議5s內

lingering_timeout

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.

能夠理解爲TCP鏈接關閉時的SO_LINGER延時設置,默認5s。 在關閉鏈接前,會檢測是否有用戶發送的數據到達服務器,若是超過lingering_timeout時間後尚未數據可讀,就直接關閉鏈接;不然,必須在讀取完鏈接緩衝區上的數據並丟棄掉後纔會關閉鏈接。

resolver_timeout

Syntax: resolver_timeout time;
Default:  resolver_timeout 30s;
Context:  http, server, location

Sets a timeout for name resolution, for example:
resolver_timeout 5s;

域名解析超時,默認30s

proxy_connect_timeout

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的鏈接超時時間, 一般不要超過75秒.默認:60s。 這個不是等待後端返回頁面的時間,那是由proxy_read_timeout聲明的。若是你的upstream服務器起來了,可是hanging住了(例如,沒有足夠的線程處理請求,因此把你的請求放到請求池裏稍後處理),那麼這個聲明是沒有用的,因爲與upstream服務器的鏈接已經創建了。

proxy_read_timeout

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等待real server響應數據的超時時間, 超時只在兩次連續的讀操做之間設置, 而不是用於傳輸整個響應。默認60s, 若是連續的60s內沒有收到1個字節, 鏈接關閉

proxy_send_timeout

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.

nginx發送數據至upstream server超時,  超時只是在兩次連續的寫操做之間設置,不是爲了整個發送期間。若是超時後,upstream沒有收到新的數據,nginx會關閉鏈接。默認60s, 若是連續的60s內沒有發送1個字節, 鏈接關閉

 

其它:

1. http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_connect_timeout

2. 499錯誤:https://aliasmee.github.io/post/questions-about-proxy-connect-timeout-on-nginx/

相關文章
相關標籤/搜索