gpt4 book ai didi

linux - 如何在haproxy中启用keep-alive?

转载 作者:可可西里 更新时间:2023-11-01 17:26:06 26 4
gpt4 key购买 nike

这是我的 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结果:

$ 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

haproxy 服务器的 curl 结果

$ 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

并且我通过 tcpdump 确认从 origin 到 haproxy 的响应中有 Connection: Keep-Alive

如您所见,haproxy 响应中没有 Connection: Keep-Alive,如何让 haproxy keep-alive??

最佳答案

HTTP 1.1 默认在保持事件模式下运行,请参阅 RFC7230 .因此,您无需明确设置连接 header 即可使用持久连接(保持事件模式)。
只有在使用 HTTP 1.0 时才需要连接 header ,因为它被设计为在每次请求后关闭连接。

因此,要验证HAProxy是否运行在Keep-alive模式,您需要使用curl发送多个HTTP请求(而不是一个),并检查是否只有一个建立和使用的连接。
serverfault post展示了如何做到这一点。 (注意 “连接到 ..”“关闭连接” 行)

关于linux - 如何在haproxy中启用keep-alive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46966813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com