haproxy 1.5之後的版本支持5種連接方式
- KAL : keep alive ("option http-keep-alive") which is the default mode : all
requests and responses are processed, and connections remain open but idle
between responses and new requests. 默認的保持連接方式
- TUN: tunnel ("option http-tunnel") : this was the default mode for versions
1.0 to 1.5-dev21 : only the first request and response are processed, and
everything else is forwarded with no analysis at all. This mode should not
be used as it creates lots of trouble with logging and HTTP processing. HTTP隧道方式
- PCL: passive close ("option httpclose") : exactly the same as tunnel mode,
but with "Connection: close" appended in both directions to try to make
both ends close after the first request/response exchange. 關閉方式
- SCL: server close ("option http-server-close") : the server-facing
connection is closed after the end of the response is received, but the
client-facing connection remains open. 服務端關閉方式
- FCL: forced close ("option forceclose") : the connection is actively closed
after the end of the response. 強制關閉方式
說下強制模式(option forceclose):
HTTP 1.1默認情況下是使用保持連接方式,只要啓用了option httpclose,服務器就會向客戶端發送Connection:close.但是某些瀏覽器不認這個http-header,依然會採用保持連接方式,這個時候服務器如果配置option forceclose就會自動在連接完成後進行關閉。
所以如果需要開啓keep-alive就只需要配置
option http-keep-alive
option http-server-close
timeout http-keep-alive 10s
timeout client 30s
#option nolinger #如果發現TIME_WAIT過多的時候開啓這個參數可以及時清理一些TCP連接,但是也會出現“連接被重置”,慎用
關閉keep-alive
option httpclose
option forceclose
#option nolinger
對比Connection:close
從上面可以看到存在150的建立連接,每次連接都請求一次
網絡層的時間達到6.425秒
Connection:keep-alive
使用保持連接方式只建立了34次請求(只用了close的五分之一),請求數量就不是存在多個,可以極大的減少建立連接的時間,減少網絡開銷,網絡層的時間也只用了4.897秒