How to enable keep-alive in haproxy?

做~自己de王妃 提交于 2019-12-11 07:25:09

问题


This is my haproxy.conf (haproxy 1.7.9)

global
    log             127.0.0.1 local0
defaults
    retries 3
    option redispatch
    timeout client  30s
    timeout connect 30s
    timeout server  30s
    option http-keep-alive
    http-reuse always

frontend web1
    bind *:8080
    option http-keep-alive
    mode http
    default_backend app1
backend app1
    balance     roundrobin
    option http-keep-alive
    mode http
    server a2 192.168.56.150:8000

curl result of origin server:

$ curl -vv http://192.168.56.150:8000/test --keepalive-time 700
* About to connect() to 192.168.56.150 port 8000 (#0)
*   Trying 192.168.56.150...
* Connected to 192.168.56.150 (192.168.56.150) port 8000 (#0)
> GET /test HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.56.150:8000
> Accept: */*
> 
< HTTP/1.1 200 OK 
< Etag: 720-6-59eeda80
< Content-Type: application/octet-stream
< Content-Length: 6
< Last-Modified: Tue, 24 Oct 2017 06:15:28 GMT
< Server: WEBrick/1.3.1 (Ruby/2.0.0/2015-12-16)
< Date: Fri, 27 Oct 2017 02:38:14 GMT
< Connection: Keep-Alive
< 
tests
* Connection #0 to host 192.168.56.150 left intact

curl result of haproxy server

$ curl -vv http://192.168.56.150:8080/test --keepalive-time 700
* About to connect() to 192.168.56.150 port 8080 (#0)
*   Trying 192.168.56.150...
* Connected to 192.168.56.150 (192.168.56.150) port 8080 (#0)
> GET /test HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.56.150:8080
> Accept: */*
> 
< HTTP/1.1 200 OK 
< Etag: 720-6-59eeda80
< Content-Type: application/octet-stream
< Content-Length: 6
< Last-Modified: Tue, 24 Oct 2017 06:15:28 GMT
< Server: WEBrick/1.3.1 (Ruby/2.0.0/2015-12-16)
< Date: Fri, 27 Oct 2017 02:38:05 GMT
< 
tests
* Connection #0 to host 192.168.56.150 left intact

And I confirmed by tcpdump that there is Connection: Keep-Alive in the response from origin to haproxy

As you can see, there's no Connection: Keep-Alive in haproxy response, how can I make haproxy keep-alive??


回答1:


HTTP 1.1 operates in keep-alive mode by default, see RFC7230. So you don't need to explicitely set the Connection header in order to use persistent connections (keep-alive mode).
The connection header is only needed when using HTTP 1.0 because it was designed to close the connection after every request.

Thus, to verify that HAProxy is operating in Keep-alive mode, you need to send multiple HTTP requests (and not just one) with curl, and check if there was only one connection established and used.
This serverfault post shows how to do that. (Notice the "Connecting to .." and "Closing connection" lines)



来源:https://stackoverflow.com/questions/46966813/how-to-enable-keep-alive-in-haproxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!