Haproxy 开日日志方法

发布时间: 更新时间: 总字数:463 阅读时间:1m 作者: IP上海 分享 网址
专栏文章
  1. Haproxy 部署和配置
  2. Haproxy 实现多域名证书 HTTPS
  3. Haproxy 开日日志方法(当前)

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

...

参考

  1. https://www.percona.com/blog/2014/10/03/haproxy-give-me-some-logs-on-centos-6-5/
  2. http://jainpiyush95.blogspot.com/2016/01/enable-logging-in-haproxy.html
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数