1. 程式人生 > >nginx 的proxy 時間講解

nginx 的proxy 時間講解

1. send_timeout

syntax: send_timeout the time

default: send_timeout 60

context: http, server, location

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.


注意fastcgi_connect_timeout fastcgi_read_timeout fastcgi_send_timeout 類似

proxy_connect_timeout 65s; #連線超時 預設為60秒
proxy_read_timeout 65s; #讀取超時 預設為60秒
proxy_send_timeout 65s; #傳送超時 預設為60秒

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.
定義用於建立與代理伺服器的連線超時。應當注意的是,此超時通常不能超過75秒。

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.
定義了從代理伺服器讀取響應超時。只是在兩個連續的讀取操作之間設定超時,而不是為整個響應的傳輸設定超時時間。如果代理伺服器在這段時間內不傳送任何東西,連線關閉。

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比較強大,可以針對單個域名請求做出單個連線超時的配置. 

比如些動態解釋和靜態解釋可以根據業務的需求配置

proxy_connect_timeout :後端伺服器連線的超時時間_發起握手等候響應超時時間

proxy_read_timeout:連線成功後_等候後端伺服器響應時間_其實已經進入後端的排隊之中等候處理(也可以說是後端伺服器處理請求的時間)

proxy_send_timeout :後端伺服器資料回傳時間_就是在規定時間之內後端伺服器必須傳完所有的資料

server
  {
 listen       80;
 server_name www.qq.cn;
 index index.jsp index.do index.html;
 root  /data/webapp/qqroot;

 #limit_conn   crawler  20;

 location /(WEB-INF)/ {
  deny all;
 }

 location / {
  proxy_pass http://192.168.1.31:8081;
  proxy_connect_timeout 500s;
  proxy_read_timeout 500s;
  proxy_send_timeout 500s;
  proxy_set_header        X-Real-IP $remote_addr;
  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header        Host $http_host;
 }

 location ~* (\.jpg)|(\.gif)|(\.png)|(\.swf)|(\.html)|(\.htm)|(\.exe)|(\.flv)|(\.doc)|(\.rar)|(\.rtf)|(\.bmp)|(\.xls)$
 {
  root /data/webapp/qqroot/;
 }

 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 {
  expires      30d;
 }


 }

proxy_connect_timeout

語法: proxy_connect_timeout timeout_in_seconds

上下文: http, server, location

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 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 the_time

預設值: proxy_read_timeout 60

上下文: http, server, location

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 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 proxyserver 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_in_seconds

預設值: proxy_send_timeout 60

上下文: http, server, location

This directive assigns timeout with the transfer of request to the proxy server. Time out is established not on entire transfer of request, but only between two operations of record. If after this time the proxy server will not take new data, then nginx is shut the connection

proxy_connect_timeout、proxy_read_timeout、proxy_send_timeout

proxy_connect_timeout  是和後端建立連線的超時時間,記住不要超過 75s 。如果超時,Nginx 的返回碼是多少? 504

proxy_read_timeout  是從後端讀取資料的超時時間,兩次讀取操作的時間間隔如果大於這個值,和後端的連線會被關閉。如果一個請求時間時間非常大,要把這個值設大點。如果超時,Nginx 的返回碼是多少? 504

proxy_send_timeout  是向後端寫資料的超時時間,兩次寫操作的時間間隔大於這個值,也就是過了這麼長時間後端還是沒有收到資料(我理解是 根據 TCP ACK 判斷),連線會被關閉。如果超時,Nginx 的返回碼是多少? 504