Haproxy开启keep-alive配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Haproxy开启keep-alive配置相关的知识,希望对你有一定的参考价值。

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秒





本文出自 “枫林晚” 博客,请务必保留此出处http://fengwan.blog.51cto.com/508652/1775083

以上是关于Haproxy开启keep-alive配置的主要内容,如果未能解决你的问题,请参考以下文章

Vue路由开启keep-alive缓存页面

客户端禁用Keep-Alive, 服务端开启Keep-Alive,会怎么样?

详解关于Vue2.0路由开启keep-alive时需要注意的地方

nginx与tomcat之间的keep-alive配置

为 EC2 实例配置 keep-alive

vue-router使用keep-alive动态缓存页面。