haproxy 常用配置项

以下的参数都是基于2.8版本测试正常使用的,其他参数请参考 haproxy 官方文档

日志格式

haproxy总更有5种日志格式。分别是default,http,https,tcp,custom
这里只说明http和tcp格式

tcp模式

global
  # 日志前台输出
  # log stdout local2 info
  # 日志输出到rsyslog。local0是设备,对应于 /etc/rsyslog.conf 中的配置,默认回收info的日志级别
  log 127.0.0.1 local0 info

# frontend/backend相关配置
frontend etcd  
  bind *:12381
  mode tcp
  # 下面两个参数是配置默认tcp格式日志
  log global
  option tcplog
  default_backend etcd

backend etcd 
  mode tcp
  server server1 127.0.0.1:2381 check

httplog默认格式:log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"

http模式

global
  # 日志前台输出
  # log stdout local2 info
  # 日志输出到rsyslog。local0是设备,对应于 /etc/rsyslog.conf 中的配置,默认回收info的日志级别
  log 127.0.0.1 local0 info

# listen相关配置
listen etcd
  bind *:12381
  mode http
  # 下面两个参数是配置默认http格式日志
  log global
  option httplog
  server server1 127.0.0.1:2381 check

tcplog默认格式:log-format "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"

常见日志定义变量

变量 含义
%ci:%cp 客户端IP:端口
[%tr] 访问时间点
%ft %b/%s frontend名称、backend名称/service名称
%TR 客户端与haproxy握手结束后,客户端向haproxy发送请求耗时
%Tw 请求在haproxy队列中等待连接槽所花费的总时间
%Tc 与后端服务三次握手的前两次握手耗时
%Tr 后端服务响应时长
%Ta 从客户端与haproxy建立tcp连接外,整个http耗时时长
%ST 状态码
%B 读取字节数
%CC 捕获的请求cookie
%CS 捕获的响应cookie
%tsc 带有cookie状态的termination_state
%fc 前端并发连接数
%bc 后端并发连接数
%sc 服务器并发连接数
%rc 重试次数
%sq 服务队列
%bq 后端队列
%hr 捕获请求头
%hs 捕获响应头
%ts 终止状态

其他详细的变量,请参见该链接

client请求时间对应的上述变量说明,请参考serverfault文档 以及 官方文档

限制请求url路径

仅http模式支持设置

http模式

# listen相关配置
listen etcd
  bind *:12381
  mode http
  # 如果访问路径不是 /metrics 的话,则返回500状态码,反之则正常访问
  http-request deny status 500 if !{ path /metrics }
  server server1 127.0.0.1:2381 check

# frontend/backend相关配置
frontend etcd  
  bind *:12381
  mode http
  http-request deny status 500 if !{ path /metrics }
  default_backend etcd

backend etcd 
  mode http
  server server1 127.0.0.1:2381 check

开启用户认证

仅http模式支持设置

http模式

global
  userlist mycredentials
    user admin password $6$tPwjkUIAl$GpdyaeYE3EYx7XQXeGt62VaqD2Rs4kyeqj27kyjTy3uSq7cgOsKIi39ENdSQbUNznEjUlotyPu4IFjxo9Zke00
    # user admin insecure-password test

# listen相关配置
listen etcd
  bind *:12381
  mode http
  # acl <aclname> <criterion>
  # auth_ok是acl名称
  # http_auth(mycredentials) 返回一个布尔值,请求从客户端接收到的身份验证数据是否与指定用户列表中存储的用户名和密码匹配。
  acl auth_ok http_auth(mycredentials)
  # 当 auth_ok 条件返回 true 时,则请求后端;否则反之
  http-request deny if !auth_ok
  # 上面两行合并成一行,实现功能一致。
  # http-request deny if !{ http_auth(mycredentials) }

  server server1 127.0.0.1:2381 check

# frontend/backend相关配置
frontend etcd  
  bind *:12381
  mode http
  acl auth_ok http_auth(mycredentials)
  http-request deny if !auth_ok
  default_backend etcd

backend etcd 
  mode http
  server server1 127.0.0.1:2381 check

userlists 是配套 listen 或者 frontend/backend 使用的

userlists 可以使用明文,也可以使用密文。密文加密方法如下:

  1. 启动mkpaaswd容器
    docker run -d --name mkpasswd jiaxzeng/mkpasswd:latest
  2. 生成密文密码
    docker exec -it mkpasswd mkpasswd -m sha-512
  3. 删除mkpaaswd容器
    docker rm -f mkpasswd

限制IP地址访问

http和tcp模式都支持设置

http模式

cat /usr/local/etc/haproxy/whitelist.lst
192.168.32.127
192.168.32.128
192.168.32.129
10.0.21.0/24

# listen相关配置
listen etcd
  bind *:12381
  mode http
  # 如果源地址不是在 whitelist.lst 文件中,则返回500状态码,反之则正常访问
  http-request deny status 500 if !{ src -f /usr/local/etc/haproxy/whitelist.lst }
  server server1 127.0.0.1:2381 check

# frontend/backend相关配置
frontend etcd  
  bind *:12381
  mode http
  http-request deny status 500 if !{ src -f /usr/local/etc/haproxy/whitelist.lst }
  default_backend etcd

backend etcd 
  mode http
  server server1 127.0.0.1:2381 check

tcp模式

cat /usr/local/etc/haproxy/whitelist.lst
192.168.32.127
192.168.32.128
192.168.32.129
10.0.21.0/24

# listen相关配置
listen etcd
  bind *:12381
  mode tcp
  # 如果源地址不是在 whitelist.lst 文件中,则连接拒绝。反之则建立连接
  tcp-request connection reject if !{ src -f /usr/local/etc/haproxy/whitelist.lst } 
  server server1 127.0.0.1:2381 check

# frontend/backend相关配置
frontend etcd  
  bind *:12381
  mode tcp
  tcp-request connection reject if !{ src -f /usr/local/etc/haproxy/whitelist.lst } 
  default_backend etcd

backend etcd 
  mode tcp
  server server1 127.0.0.1:2381 check
posted @   jiaxzeng  阅读(194)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示