将Apache日志输出到Syslog服务器
将Apache日志输出到Syslog服务器

将Apache日志输出到Syslog服务器

内容纲要

将Apache日志输出到Syslog服务器

背景

工作上希望我能够将Apache的日志输出到Syslog服务器,也就是说需要日志先经过Syslog在输出出来,查阅一些资料后,进行了一些测试和配置最后成功了所以记录一下。

前提条件

Apache服务器(Redhat 8)和Syslog服务器

在此基础上,修改Apache服务器的配置

步骤

/etc/httpd/conf/httpd.conf

首先是httpd.conf:

## 略

    #
    # If you prefer a logfile with access, agent, and referer information 
    # (Combined Logfile Format) you can use the following directive.
    ## 修改↓
    ## CustomLog "logs/access_log" combined  
    CustomLog "|/usr/bin/logger -p local5.info -t httpd_access" combined
    ## 我们自己定义一下输出的格式↓
LogFormat "%{%Y-%m-%d %H:%M:%S}t %{x-forwarded-for}i %>s %m %U%q %{Referer}i %{User-Agent}i %B %{Cookie}i" combined

## 略

#
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost> 
# container, that host's errors will be logged there and not here.
#
## ↓修改
## ErrorLog "logs/error_log"
ErrorLog  "|/usr/bin/logger -p local6.info -t error"

## 略

将普通日志(CustomLog)和错误日志(ErrorLog)改为通过logger输出。

到这里Apache端设置完成。

/etc/rsyslog.conf

接着修改rsyslog的配置:

## 略
## ↓修改
## *.info;mail.none;authpriv.none;cron.none                /var/log/messages
*.info;mail.none;authpriv.none;cron.none;local3.none;local4.none;local5.none;local6.none;                /var/log/messages

## 略

##新增 ↓
# httpd access log  
local5.*                                              @Syslog服务器IP
local5.*                                              /var/log/httpd/access_log
# httpd error log
local6.*                                              @Syslog服务器IP
local6.*                                              /var/log/httpd/error_log

## 略

之前的设置会使Apache的日志也输出到/var/log/messages,所以添加local3.none等过滤不输出到该文件。

另外新增针对各facility输出到外部Syslog服务器@192.168.1.1的设置,同时保留输出到本地的日志。

重启rsyslog和httpd

systemctl restart rsyslog
systemctl restart httpd

最后重启rsyslog和Apache。

这样日志就会输出到Syslog服务器,同时本地日志也继续正常记录。

参考

CentOS7 で Apache のログを syslog に流す
Apacheのログをsyslog(rsyslog)で管理する

一条评论

  1. Pingback:rsyslog日志格式以及自动换行,丢失问题的解决记录📝 – Tutu的个人博客

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注