nginx 日志切割

  • 日志,nginx,crontab,logrotate,anacrontab,nginx日志切割,nginx日志打包,logrotate日志,anacrontab定时任务
  • 2018-01-25 07:24:12

在新的服务器上搭建网站已经有一小段时间了,这几天心血来潮,想看一下服务器的访问日志情况。前天写了一个小脚本,分析 nginx 的 access.log 日志。然后写了一个定时脚本,每天 03:30 的时候分析 access.log 然后输出一个文档。为什么是 3:30 呢,因为我看见日志目录下,每天的三点半多一些的时候,会自动生成一个 access.log-201ymmdd.gz 的文件。于是我在这个时间点之前分析一下,虽然不是很精确,但是那点误差我还是可以接收的。

但是很不幸,这两天发现分析的数据非常少,看一眼 access.log ,惊呆了,时间提前了,那么我三点半执行的脚本可不就是只分析了那么几分钟的访问量嘛。

[root@izj6cftv2j5c551cazkn5ez nginx]# ll /var/log/nginx
-rw-r--r-- 1 nginx nginx 308404 1月  12 12:01 access.log
-rw-r--r-- 1 nginx nginx  88204 1月  10 03:15 access.log-20180110.gz
-rw-r--r-- 1 nginx nginx  95550 1月  11 03:30 access.log-20180111.gz
-rw-r--r-- 1 root  root   69375 1月  12 03:07 access.log-20180112.gz

对于这个自动切割,打包压缩日志的功能,我得搞明白它,不然我怎么确定我的定时脚本的时间啊。网上搜了以下,关于 nginx 自动切割打包压缩日志的功能,都说 nginx 自身并不带这么一个功能,有很多教程在教你怎么写这么一个脚本来实现这个功能。但是我并没有写过啊,查看了一下 nginx 和 root 用户的 crontab ,也都没有这么一个脚本存在。

[root@izj6cftv2j5c551cazkn5ez nginx]# crontab -l -u nginx
no crontab for nginx

也有网友说是新版本的 nginx 有自带日志按天切割,压缩打包的功能,但是我的 nginx 的官网上并没有发现这样的描述,也没有找到对应设置的说明文档。nginx 推出这么一个功能肯定会有配套的说明文档的,也需要有对应的配置说明的,不然这个弹性很大的时间范围,不明显不适合在生产环境中使用的。

再接着查,后来查到这么一个工具, Logrotate ,Logrotate 是一个日志工具。相比之前网友分享的日志切割脚本,这个会更加完美一些。比如要保证日志切割的时候,不能影响日志的写入。这样的功能在简单的脚本上肯定会有些问题,但是毕竟大家的访问量并没有那么高,精确要求也没有那么高,所以也可以接受。Logrotate 是一个日志工具,它的执行还是需要借助 cron 。在 /etc/ 目录下,发现了这么几个目录

drwxr-xr-x.  2 root root     4096 10月 15 23:24 cron.d
drwxr-xr-x.  2 root root     4096 10月 15 23:19 cron.daily
-rw-------.  1 root root        0 8月   3 23:33 cron.deny
drwxr-xr-x.  2 root root     4096 10月 15 23:19 cron.hourly
drwxr-xr-x.  2 root root     4096 6月  10 2014 cron.monthly

很明显,cron.daily 是每日的脚本,在 /etc/cron.daily/ 目录下,我们发现了 logrotate 文件,绝对位置为 /etc/cron.daily/logrotate。文件如下

[root@izj6cftv2j5c551cazkn5ez cron.daily]# cat /etc/cron.daily/logrotate 
#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

虽然看不大懂说明意思,但是大致明白这里干了一件重要的事情,执行 logrotate logrotate.conf。接下来,就是看一下 logrotate.conf 里是什么。

[root@izj6cftv2j5c551cazkn5ez cron.daily]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
    minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}
# system-specific logs may be also be configured here.

这里好像没有咱们想要的脚本,但是发现 include /etc/logrotate.d 这行代码,很熟悉。跟我们的 nginx 的 conf.d 一个套路,我们看一下这个目录里有啥。

[root@izj6cftv2j5c551cazkn5ez logrotate.d]# ll /etc/logrotate.d/
-rw-r--r--. 1 root root  76 8月   2 00:24 bootlog
-rw-r--r--. 1 root root 160 1月  31 2017 chrony
-rw-r--r--  1 root root 194 10月 20 00:44 httpd
-rw-r--r--  1 root root 844 9月  14 00:27 mysql
-rw-r--r--  1 root root 243 10月 18 16:08 nginx
-rw-r--r--  1 root root 203 11月 16 00:36 php-fpm
-rw-r--r--. 1 root root 224 5月  10 2017 syslog
-rw-r--r--. 1 root root 100 8月   4 14:47 wpa_supplicant
-rw-r--r--. 1 root root 100 8月   6 03:13 yum

这个 nginx 文件应该就是咱们关心的内容了,打开看看。

[root@izj6cftv2j5c551cazkn5ez logrotate.d]# cat /etc/logrotate.d/nginx 
/var/log/nginx/*log {
    create 0644 nginx nginx
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

这个配置文件大概就是告诉 logrotate 怎么处理 /var/log/nginx/*log 的文件了。但是这里并没有指出执行脚本的时间点,是说了频率是 daily 。

logrotate 是一个切割日志的工具,执行脚本的时间点的事情,应该是 cron 来负责的。所以我们再去看看 cron 。

在网上查了一下。cron 有个配置文件 /etc/crontab,但是在这个文件里,并没有发现什么。

[root@izj6cftv2j5c551cazkn5ez etc]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

在新版的 centOS 中,配置文件为 /etc/anacrontab,文件中有 START_HOURS_RANGE=3-22 ,表明日常脚本的执行时间段。

[root@izj6cftv2j5c551cazkn5ez etc]# cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5   cron.daily      nice run-parts /etc/cron.daily
7       25  cron.weekly     nice run-parts /etc/cron.weekly
@monthly     45 cron.monthly        nice run-parts /etc/cron.monthly

到这里,大概就明白了,在 /etc/anacrontab 中,设置了每日定时脚本启动的时间,在 3 点到 22 点之间,每日日志脚本切割和压缩打包的工具是 logrotate 。和 nginx 本身可能没有什么特别的关系。

相关文章

- EOF -

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动。
转载请注明:文章转载自 Binkery 技术博客 [https://binkery.com]
本文标题: nginx 日志切割
本文地址: https://binkery.com/archives/595.html