nginx.conf 面試題 timeout超時時

nginx常用的超時配置說明

client_header_timeout

語法 client_header_timeout time
默認值 60s
上下文 http server(指可以放在http塊和server塊)
說明 指定等待client發送一個請求頭的超時時間(例如:GET / HTTP/1.1).僅當在一次read中,沒有收到請求頭,纔會算成超時。如果在超時時間內,client沒發送任何東西,nginx返回HTTP狀態碼408(“Request timed out”)

client_body_timeout 
語法 client_body_timeout time
默認值 60s
上下文 http server location
說明 該指令設置請求體(request body)的讀超時時間。僅當在一次readstep中,沒有得到請求體,就會設爲超時。超時後,nginx返回HTTP狀態碼408(“Request timed out”)

keepalive_timeout (長連接類型)
語法 keepalive_timeout timeout [ header_timeout ]
默認值 75s
上下文 http server location
說明 第一個參數指定了與client的keep-alive連接超時時間。服務器將會在這個時間後關閉連接。可選的第二個參數指定了在響應頭Keep-Alive: timeout=time中的time值。這個頭能夠讓一些瀏覽器主動關閉連接,這樣服務器就不必要去關閉連接了。沒有這個參數,nginx不會發送Keep-Alive響應頭(儘管並不是由這個頭來決定連接是否“keep-alive”)(服務器在返回數據給用戶時,在頭header文件中會添加keepalive字段,75s,瀏覽器在這個時間後能夠主動關閉連接)
兩個參數的值可並不相同

  • 注意不同瀏覽器怎麼處理“keep-alive”頭

  • MSIE和Opera忽略掉"Keep-Alive: timeout=<N>" header.

  • MSIE保持連接大約60-65秒,然後發送TCP RST

  • Opera永久保持長連接

  • Mozilla keeps the connection alive for N plus about 1-10 seconds.

  • Konqueror保持長連接N秒


lingering_timeout
語法 lingering_timeout time
默認值 5s
上下文 http server location
說明 lingering_close生效後,在關閉連接前,會檢測是否有用戶發送的數據到達服務器,如果超過lingering_timeout時間後還沒有數據可讀,就直接關閉連接;否則,必須在讀取完連接緩衝區上的數據並丟棄掉後纔會關閉連接。

resolver_timeout
語法 resolver_timeout time 
默認值 30s
上下文 http server location
說明 該指令設置DNS解析超時時間

proxy轉發模塊的超時設置:
proxy_connect_timeout
語法 proxy_connect_timeout time 
默認值 60s
上下文 http server location
說明 該指令設置與upstream server的連接超時時間,有必要記住,這個超時不能超過75秒。
這個不是等待後端返回頁面的時間,那是由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.


proxy_read_timeout
語法 proxy_read_timeout time 
默認值 60s
上下文 http server location
說明 該指令設置與代理服務器的讀超時時間。它決定了nginx會等待多長時間來獲得請求的響應。這個時間不是獲得整個response的時間,而是兩次reading操作的時間。(??什麼是兩次reading操作的時間)

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.


proxy_send_timeout
語法 proxy_send_timeout time 
默認值 60s
上下文 http server location
說明 這個指定設置了發送請求給upstream服務器的超時時間。超時設置不是爲了整個發送期間,而是在兩次write操作期間。如果超時後,upstream沒有收到新的數據,nginx會關閉連接

proxy_upstream_fail_timeout(fail_timeout)
語法 server address [fail_timeout=30s]
默認值 10s
上下文 upstream
說明 Upstream模塊下 server指令的參數,設置了某一個upstream後端失敗了指定次數(max_fails)後,該後端不可操作的時間,默認爲10秒

 

負載均衡配置時的2個參數:fail_timeout和max_fails

   這2個參數一起配合,來控制nginx怎樣認爲upstream中的某個server是失效的當在fail_timeout的時間內,某個server連接失敗了max_fails次,則nginx會認爲該server不工作了。同時,在接下來的 fail_timeout時間內,nginx不再將請求分發給失效的server。
個人認爲,nginx不應該把這2個時間用同一個參數fail_timeout來控制,要是能再增加一個fail_time,來控制接下來的多長時間內,不再使用down掉的server就更好了~
如果不設置這2個參數,fail_timeout默認爲10s,max_fails默認爲1。就是說,只要某個server失效一次,則在接下來的10s內,就不會分發請求到該server上


 

 

send_timeout

默認:send_timeout 60;

send_timeout 指定客戶端的響應超時時間。這個設置不會用於整個轉發器,而是在兩次客戶端讀取操作之間。如果在這段時間內,客戶端沒有讀取任何數據,nginx就會關閉連接。

Directive assigns response timeout to client. Timeout is established not on entire transfer of answer, but only between two operations of reading, if after this time client will take nothing, then nginx is shutting down the connection.

 

 

另一個參考:504 Gateway Time-out問題

常見於使用nginx作爲web server的服務器的網站

我遇到這個問題是在升級discuz論壇的時候遇到的

一般看來, 這種情況可能是由於nginx默認的fastcgi進程響應的緩衝區太小造成的, 這將導致fastcgi進程被掛起, 如果你的fastcgi服務對這個掛起處理的不好, 那麼最後就極有可能導致504 Gateway Time-out
現在的網站, 尤其某些論壇有大量的回覆和很多內容的, 一個頁面甚至有幾百K
默認的fastcgi進程響應的緩衝區是8K, 我們可以設置大點
在nginx.conf裏, 加入:

fastcgi_buffers 8 128k

這表示設置fastcgi緩衝區爲8×128k
當然如果您在進行某一項即時的操作, 可能需要nginx的超時參數調大點, 例如設置成60秒:

send_timeout 60;

    調整了這兩個參數, 結果就是沒有再顯示那個超時, 可以說效果不錯, 但是也可能是由於其他的原因, 目前關於nginx的資料不是很多, 很多事情都需要長期的經驗累計纔有結果。

 

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 32 4k;
proxy_busy_buffers_size 64k;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章