Haproxy作为web服务器,记录访问日志是十分必要的。下面介绍如何将Haproxy的日志模块打开,并将日志发送到syslog中
haproxy 开日日志方法
注:考虑性能等因素,建议使用在测试环境。
配置 haproxy
配置 /etc/haproxy/haproxy.cfg
,添加如下:
global
log 127.0.0.1 local2 [emerg|alert|crit|err|warning|notice|info|debug]
配置 rsyslog
配置 /etc/sysconfig/rsyslog
,添加-r
参数:
# SYSLOGD_OPTIONS="" ## 修改前配置
SYSLOGD_OPTIONS="-c 2 -r -m 0"
SYSLOGD_OPTIONS 参数说明:
- -c 2 使用兼容模式,默认是 -c 5
- -r 开启远程日志
- -m 0 标记时间戳。单位是分钟,为0时,表示禁用该功能
配置 /etc/rsyslog.conf
,取消注释:
$ModLoad imudp
$UDPServerRun 514
解除注释:
$IncludeConfig /etc/rsyslog.d/*.conf
创建新 haproxy
配置文件 /etc/rsyslog.d/haproxy.conf
,内容如下:
vim /etc/rsyslog.d/haproxy.conf
# Save haproxy log
local2.* /var/log/haproxy.log
重启服务
systemctl restart rsyslog.service
systemctl restart haproxy.service
上述方法,参考haproxy 默认配置文件haproxy.cfg,内容如下:
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
...