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

Haproxy开启keep-alive配置_第1张图片

从上面可以看到存在150的建立连接,每次连接都请求一次

网络层的时间达到6.425秒


Connection:keep-alive

Haproxy开启keep-alive配置_第2张图片

Haproxy开启keep-alive配置_第3张图片


使用保持连接方式只建立了34次请求(只用了close的五分之一),请求数量就不是存在多个,可以极大的减少建立连接的时间,减少网络开销,网络层的时间也只用了4.897秒