一、啥時候用到
用來佈建要求資源和伺服器返回的時間,保證一個請求佔用固定時間,超出後報504逾時!這樣可以保證一個請求佔用過長時間。
二、主要參數
使用nginx伺服器如果遇到timeou情況時可以如下設定參數,使用fastcgi:
fastcgi_connect_timeout 75; 連結
fastcgi_read_timeout 600; 讀取
fastcgi_send_timeout 600; 發請求
這兩個選項.
fastcgi_read_timeout是指fastcgi進程向nginx進程發送response的整個過程的逾時時間
fastcgi_send_timeout是指nginx進程向fastcgi進程發送request的整個過程的逾時時間
這兩個選項預設都是秒(s),可以手動指定為分鐘(m),小時(h)等
三、其他常用參數以及參數說明
keepalive_timeout 600; 連線逾時時間,1分鐘,具體時間可以根據請求(例如後台匯入)需要的時間來設定
proxy_connect_timeout 600; 1分鐘
proxy_read_timeout 600; 1分鐘
nginx逾時配置參數說明:
keepalive_timeout
文法 keepalive_timeout timeout [ header_timeout ]
預設值 75s
上下文 http server location
說明 第一個參數指定了與client的keep-alive連線逾時時間。伺服器將會在這個時間後關閉串連。可選的第二個參數指定了在回應標頭Keep-Alive: timeout=time中的time值。這個頭能夠讓一些瀏覽器主動關閉串連,這樣伺服器就不必要去關閉串連了。沒有這個參數,nginx不會發送Keep-Alive回應標頭(儘管並不是由這個頭來決定串連是否“keep-alive”)
兩個參數的值可並不相同
注意不同瀏覽器怎麼處理“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秒
proxy_connect_timeout
文法 proxy_connect_timeout time
預設值 60s
上下文 http server location
說明 該指令設定與upstream server的連線逾時時間,有必要記住,這個逾時不能超過75秒。
這個不是等待後端返回頁面的時間,那是由proxy_read_timeout聲明的。如果你的upstream伺服器起來了,但是hanging住了(例如,沒有足夠的線程處理請求,所以把你的請求放到請求池裡稍後處理),那麼這個聲明是沒有用的,由於與upstream伺服器的串連已經建立了。
proxy_read_timeout
文法 proxy_read_timeout time
預設值 60s
上下文 http server location
說明 該指令設定與Proxy 伺服器的讀逾時時間。它決定了nginx會等待多長時間來獲得請求的響應。這個時間不是獲得整個response的時間,而是兩次reading操作的時間。
client_header_timeout
文法 client_header_timeout time
預設值 60s
上下文 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”)
lingering_timeout
文法 lingering_timeout time
預設值 5s
上下文 http server location
說明 lingering_close生效後,在關閉串連前,會檢測是否有使用者發送的資料到達伺服器,如果超過lingering_timeout時間後還沒有資料可讀,就直接關閉串連;否則,必須在讀取完串連緩衝區上的資料並丟棄掉後才會關閉串連。
resolver_timeout
文法 resolver_timeout time
預設值 30s
上下文 http server location
說明 該指令設定DNS解析逾時時間
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秒
四、執行個體
這裡來看一個把Nginx的逾時時間上調的例子。
看看時間是否符合要求,在nginx.config裡面的三個參數:
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
以上的單位是秒。
如果使用了Nginx的代理,可以在塊裡加上:
proxy_connect_timeout 300s;proxy_send_timeout 300s;proxy_read_timeout 300s;
變成:
location /foo { proxy_pass http://xxx.xxx.xxx.xxx:8080/foo; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; access_log /var/log/nginx/access.foo.log main; error_log /var/log/nginx/error.foo.log;}