Nginx会把access和error日志记录到文件中,使用Syslog进程(如:rsyslog)可以监控这些日志并且将他们发送到Loggly中,下面就是如何使用Loggly来纪录nginx的日志。本例中的rsylog使用1.19或更高版本,使用TCP 514端口,Ubuntu上默认的Nginx日志目录和标准的日志格式。
自动安装脚本
1. 运行Nginx配置脚本
运行如下脚本来搭建Nginx日志并传输到Loggly上。
curl -O https://www.loggly.com/install/configure-nginx.sh
sudo bash configure-nginx.sh -a SUBDOMAIN -u USERNAME
curl -O https://www.loggly.com/install/configure-nginx.sh sudo bash configure-nginx.sh -a SUBDOMAIN -u USERNAME |
说明:
SUBDOMAIN 是你在Loggly上面注册的子域名
USERNAME 是你的Loggly的用户名
2. 确认事件
在Loggly上确认nginx标签是否已经打上。可能需要换上几分钟同步,如果没有可以按照如下步骤进行troubleshooting。
tag:nginx
手动配置
1. 配置Syslog Daemon
curl -O https://www.loggly.com/install/configure-linux.sh
sudo bash configure-linux.sh -a SUBDOMAIN -u USERNAME
curl -O https://www.loggly.com/install/configure-linux.sh sudo bash configure-linux.sh -a SUBDOMAIN -u USERNAME |
同样:
SUBDOMAIN 是你在Loggly上面注册的子域名
USERNAME 是你的Loggly的用户名
2. 配置Nginx文件监控
创建或者打开rsyslog配置文件:
sudo vim /etc/rsyslog.d/21-nginx-loggly.conf
sudo vim /etc/rsyslog.d/21-nginx-loggly.conf |
将下面的部分添加进去:
$ModLoad imfile
$InputFilePollInterval 10
$PrivDropToGroup adm
$WorkDirectory /var/spool/rsyslog
# Nginx access file:
$InputFileName /var/log/nginx/access.log
$InputFileTag nginx-access:
$InputFileStateFile stat-nginx-access
$InputFileSeverity info
$InputFilePersistStateInterval 20000
$InputRunFileMonitor
#Nginx Error file:
$InputFileName /var/log/nginx/error.log
$InputFileTag nginx-error:
$InputFileStateFile stat-nginx-error
$InputFileSeverity error
$InputFilePersistStateInterval 20000
$InputRunFileMonitor
#Add a tag for nginx events
$template LogglyFormatNginx,"<%pri%>%protocol-version% %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [[email protected] tag="nginx"] %msg%
"
if $programname == 'nginx-access' then @@logs-01.loggly.com:514;LogglyFormatNginx
if $programname == 'nginx-access' then ~
if $programname == 'nginx-error' then @@logs-01.loggly.com:514;LogglyFormatNginx
if $programname == 'nginx-error' then ~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$ModLoad imfile $InputFilePollInterval 10 $PrivDropToGroup adm $WorkDirectory /var/spool/rsyslog # Nginx access file: $InputFileName /var/log/nginx/access.log $InputFileTag nginx-access: $InputFileStateFile stat-nginx-access $InputFileSeverity info $InputFilePersistStateInterval 20000 $InputRunFileMonitor #Nginx Error file: $InputFileName /var/log/nginx/error.log $InputFileTag nginx-error: $InputFileStateFile stat-nginx-error $InputFileSeverity error $InputFilePersistStateInterval 20000 $InputRunFileMonitor #Add a tag for nginx events $template LogglyFormatNginx,"%protocol-version% %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [[email protected] tag="nginx"] %msg% "if $programname == 'nginx-access' then @@logs-01.loggly.com:514;LogglyFormatNginx if $programname == 'nginx-access' then ~ if $programname == 'nginx-error' then @@logs-01.loggly.com:514;LogglyFormatNginx if $programname == 'nginx-error' then ~ |
说明:
将TOKEN替换为你在Loggly上面的customer token。
重启rsyslogd
$ sudo service rsyslog restart
3. 确认事件