haproxy日志配置
配置HAProxy记录日志到指定日志文件中
在global配置项定义
log 127.0.0.1 local3 info
log 127.0.0.1 local{1-7} info #基于syslog记录日志到指定设备,级别有(err、warning、info、debug)
引用global
listen web_port
bind 192.168.1.101:80
mode http
log global
server web1 192.168.1.103:80 check inter 3000 fall 2 rise 5
Rsyslog配置
root@z1:~# vim /etc/rsyslog.conf
# provides UDP syslog reception
module(load="imudp")
input(type="imudp" port="514")
local3.* /var/log/haproxy.log
访问验证是否生成log 文件
root@z1:~# systemctl restart haproxy
root@z1:~# systemctl restart rsyslog
root@z1:~# tail -f /var/log/haproxy.log
2020-03-31T14:09:26+08:00 localhost haproxy[1892]: Connect from 192.168.1.105:45418 to 192.168.1.101:80 (web_port/HTTP)
自定义日志格式
将特定信息记录在日志中
配置选项
capture cookie <name> len <length> #捕获请求和响应报文中的 cookie并记录日志
capture request header <name> len <length> #捕获请求报文中指定的首部内容和长度并记录日志
capture response header <name> len <length> #捕获响应报文中指定的内容和长度首部并记录日志
示例:
capture request header Host len 256
capture request header User-Agent len 512
capture request header Referer len 15
配置示例
listen web_host
bind 192.168.1.101:80
mode http
balance roundrobin
log global
option httplog #日志格式选项
capture request header X-Forwarded-For len 15
capture request header User-Agent len 512
cookie SERVER-COOKIE insert indirect nocache
server web1 192.168.1.103:80 cookie web1 check inter 3000 fall 3 rise 5
server web2 192.168.1.104:80 cookie web2 check inter 3000 fall 3 rise 5
验证日志格式
注意:
虽然可以记录日志,但是一般不在haproxy 上记录日志。通常在后端web 服务器上记录,记得更详细。在haproxy 记日志是为了debug 排错用