如何在与前端相同的端口上的 HAProxy 1.6.4 中 运行 统计信息?

How to run stats in HAProxy 1.6.4 on same port as frontend?

我正在使用 HAProxy 1.6.4 并希望启用统计信息。 (/haproxy?stats)

这是我的配置文件:

global
   log 127.0.0.1 local2
   daemon
   maxconn 256

defaults
   log global
   timeout connect  5000
   timeout client  10000
   timeout server  10000

frontend http-in
   bind *:8080
   default_backend testb

backend testb
   balance roundrobin
   server s1 123.456.789.0:443 maxconn 32
   server s2 123.456.789.1:443 maxconn 32

listen statistics
   bind *:8080
   mode http
   stats enable

如果我 运行 在 8080 以外的其他端口上进行统计,它可以工作,但我如何 运行 它与我的前端 (8080) 相同的端口,即 运行ning默认 mode tcp?

这只是一个有根据的猜测。您无法在 tcp 模式下提供 stats 页面,因为它在第 4 层进行代理。在这种模式下,haproxy 只知道来自传入数据包的 IP 和端口,并根据定义的规则相应地路由它。

http 模式(第 7 层)不同,它有更多信息可以像 HTTP headers 一样工作,其中 path 可用并使用它来了解何时为 /haproxy?stats页。

您可以通过重定向到您自己并使用如下访问列表来实现:

global
   log 127.0.0.1 local2
   daemon
   maxconn 256

defaults
   log global
   timeout connect  5000
   timeout client  10000
   timeout server  10000

listen stats :1936
   mode http
   stats enable
   stats hide-version
   stats realm Haproxy\ Statistics
   stats uri /
   stats auth myUser:myPassword

frontend http-in
   bind *:8080
   acl is_www hdr_end(host) -i www.mysite.com
   acl is_stat hdr_end(host) -i stat.mysite.com  

   use_backend srv_www if is_www
   use_backend srv_stat if is_stat

backend srv_www
   balance roundrobin
   server s1 123.456.789.0:443 maxconn 32
   server s2 123.456.789.1:443 maxconn 32

backend srv_stat
   server Local 127.0.0.1:1936

当使用 www 访问您的服务器时,它会将您带到网络服务器。 但是使用 stat,它会将您从输入端口 8080 重定向到 1936,而统计数据是 运行

如果您乐于使用自己的路径,那真的很容易。 这应该有效

global
   log 127.0.0.1 local2
   daemon
   maxconn 256

defaults
   log global
   timeout connect  5000
   timeout client  10000
   timeout server  10000

frontend http-in
   bind *:8080

   stats enable
   stats uri /stats

   default_backend testb

backend testb
   balance roundrobin
   server s1 123.456.789.0:443 maxconn 32
   server s2 123.456.789.1:443 maxconn 32

然后您可以访问 haproxy 统计信息 http://<主机名>:8080/stats

(在 haproxy 2.5.5 上测试)