HAProxy配置记录日志

option httplog 可以将http格式记录下,并且可以使用相关指令将特定信息记录在haproxy的日志中但一般不建议开启,这会加重 HAProxy 负载
HAProxy配置


自定义日志格式:
选项:

log IP local{1-7} info                    #global配置项,基于syslog记录日志到指定设备,级别有(err、warning、info、debug)
log global                           #开启记录日志,默认不开启
option httplog #                 开启记录httplog日志格式选项
capture cookie <name> len <length>          #捕获请求和响应报文中的 cookie并记录日志
capture request header <name> len <length>  #捕获请求报文中指定的首部内容和长度并记录日志
capture response header <name> len <length> #捕获响应报文中指定的内容和长度首部并记录日志



HAProxy日志配置:
在global配置项定义:
log 127.0.0.1 local{1-7} info #基于syslog记录日志到指定设备,级别有(err、warning、info、debug)
log 192.168.80.120 local3 info #远程日志服务器

listen web_host
bind 10.0.0.7:80
mode http
balance roundrobin
log global                        #开启日志功能
option httplog                       #开启httplog日志格式选项
capture request header User-Agent len 512         #记录日志信息
capture request header Host len 256                #记录日志信息
cookie SERVER-COOKIE insert indirect nocache
server web1 10.0.0.17:80 cookie web1 check inter 3000 fall 3 rise 5
server web2 10.0.0.27:80 cookie web2 check inter 3000 fall 3 rise 5


# systemctl restart haproxy

Rsyslog配置

#开启本地日志
vim /etc/rsyslog.conf
  $ModLoad imudp #开启UPD监听
  $UDPServerRun 514 #开启UPD监听
  local3.* /var/log/haproxy.log #日志路径
  systemctl restart rsyslog

Rsyslog配置:
#开启远程主机日志
vim /etc/rsyslog.conf
  module(load="imudp")
  input(type="imudp" port="514")
  local3.* /var/log/haproxy.log
  systemctl restart rsyslog

 

 

curl http://192.168.80.110:9999/haproxy-status

访问测试:

[root@localhost7B haproxy-2.0.22]# tail -f /var/log/haproxy.log
Jul 22 19:01:25 localhost haproxy[60334]: Connect from 192.168.80.1:55868 to 192.168.80.110:9999 (stats/HTTP)
Jul 22 19:01:25 localhost haproxy[60334]: Connect from 192.168.80.1:55869 to 192.168.80.110:9999 (stats/HTTP)
Jul 22 19:01:25 localhost haproxy[60334]: Connect from 192.168.80.1:55869 to 192.168.80.110:9999 (stats/HTTP)

 

Jul 22 20:30:12 localhost haproxy[62119]: 192.168.80.1:55340 [22/Jul/2022:20:30:12.055] stats stats/<STATS> 0/0/0/1/1 200 36635 - - LR-- 2/2/0/0/0 0/0 "GET /haproxy-status HTTP/1.1"
Jul 22 20:30:12 localhost haproxy[62119]: 192.168.80.1:55340 [22/Jul/2022:20:30:12.107] stats stats/<NOSRV> 0/-1/-1/-1/0 503 237 - - SC-- 2/2/0/0/0 0/0 "GET /favicon.ico HTTP/1.1"
Jul 22 20:31:26 localhost haproxy[62119]: 192.168.80.1:55341 [22/Jul/2022:20:30:12.054] stats stats/<NOSRV> -1/-1/-1/-1/74673 400 211 - - CR-- 1/1/0/0/3 0/0 "<BADREQ>"

posted @ 2022-07-22 19:05  yuanbangchen  阅读(1480)  评论(0编辑  收藏  举报