Nginx使用Logrotate工具实现日志切割

1,101 阅读4分钟

日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到。对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题。除此之外,处理一个单个的庞大日志文件也常常是件十分棘手的事。 Nginx日志切割方式有很多种,本章节拿出两种案例来进行分享,分别为 Logrotate 工具切割和脚本切割。

Logrotate切割Nginx日志

切割方式一

[root@nginx_proxy02 /]# vim /etc/logrotate.d/nginx
#按天复制清空切割
/usr/local/nginx/logs/*.log {
        daily                               
        rotate 30
        missingok
        notifempty
        compress
        nodelaycompress
        copytruncate
        dateext
        dateformat -%Y-%m-%d
        dateyesterday
}

配置解析:
#按天切割|保留30个日志文件|切割中遇到日志错误忽略|日志如果为空将不进行切割和压缩|
#以gzip方式压缩|不要将刚切割后的日志文件放到下个循环中进行压缩|复制源日志文件后并清空源日志文件|
#切割后的文件添加日志扩展名|扩展方式为 -%Y-%m-%d|切割后的文件日志命名为昨天的日期|

#手动执行切割(我们做测试使用)
1.写入30M内容
[root@nginx_proxy02 logs]# head -c 30M < /dev/urandom > /usr/local/nginx/logs/error.log
[root@nginx_proxy02 logs]# head -c 30M < /dev/urandom > /usr/local/nginx/logs/access.log
[root@nginx_proxy02 logs]# head -c 30M < /dev/urandom > /usr/local/nginx/logs/access.json

2.强制执行切割(不管是否到了任务计划自动执行时间)
[root@nginx_proxy02 logs]# logrotate -vf /etc/logrotate.d/nginx
-v 显示切割过程中的详细信息
-f 强制执行切割

3.查看切割效果
[root@nginx_proxy02 logs]# ls -lrth
total 91M
-rw-r--r-- 1 nginx root 31M Mar 25 17:09 error.log-2020-03-24.gz
-rw-r--r-- 1 nginx root 31M Mar 25 17:09 access.log-2020-03-24.gz
-rw-r--r-- 1 nginx root 30M Mar 25 17:09 access.json
-rw-r--r-- 1 nginx root   0 Mar 25 17:11 access.log
-rw-r--r-- 1 nginx root   0 Mar 25 17:11 error.log

切割方式二

[root@nginx_proxy02 /]# vim /etc/logrotate.d/nginx
#向Nginx发送信号重新打开日志文件切割
/usr/local/nginx/logs/*.log {
        daily
        rotate 30
        missingok
        notifempty
        compress
        nodelaycompress
        dateext
        dateformat -%Y-%m-%d
        dateyesterday
        postrotate
            if [ -f /usr/local/nginx/run/nginx.pid ];then
                kill -USR1 `cat /usr/local/nginx/run/nginx.pid`
            fi
        endscript
}

配置解析:
#按天切割|保留30个日志文件|切割中遇到日志错误忽略|日志如果为空将不进行切割和压缩|
#以gzip方式压缩|不要将刚切割后的日志文件放到下个循环中进行压缩|
#切割后的文件添加日志扩展名|扩展方式为 -%Y-%m-%d|切割后的文件日志命名为昨天的日期|
#在切割后执行 postrotate / endscript 之间的命令,向nginx发送信号重新打开日志

切割方式三

#不同格式文件大小切割
"/usr/local/nginx/logs/access.log" "/usr/local/nginx/logs/error.log" /usr/local/nginx/logs/access.json {
        size 100M
        rotate 30
        missingok
        notifempty
        compress
        nodelaycompress
        copytruncate
        dateext
        dateformat -%Y-%m-%d-%H
}

配置解析:
与上两种方法区别为把 daily 参数改为了 size 参数,取消了 dateyesterday 参数

切割时间 上面讲logrotate按时间切割有 天|周|月|年,可具体时间是几点钟? logrotate默认的定时任务放在了 /etc/cron.daily/logrotate 文件中

[root@nginx_proxy02 logs]# 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

可是上面脚本中只写入了执行 /usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf logotate的命令,然后下面有一个判断执行是否成功,并没有时间,这该去哪里找? logrotate的行为也是受crontab控制,而crontab任务是手anacron控制,所以来看anacrontab

[root@nginx_proxy02 logs]# 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

所有系统级别的计划任务都是在上面脚本 START_HOURS_RANGE= 指定的时间点内执行,3-22 表示每天的凌晨 3点-22点内,随机执行,是不是很扯淡,别着急往下看。 我们可以修改 START_HOURS_RANGE= 23-24 (这是日志切割时间点,即只在23点到24点开始切割,不出意外就是23点啦),当然我们可以自己写crontab任务,不过要把 logrotate 的任务计划给删掉。然后在每天凌晨00:00进行切割nginx日志

[root@nginx_proxy02 logs]# rm -rf /etc/cron.daily/logrotate
[root@nginx_proxy02 logs]# crontab -e
#Nginx logs cut
00 00 * * * /sbin/logrotate -f /etc/logrotate.d/nginx

※更多文章和资料|点击后方文字直达 ↓↓↓ 100GPython自学资料包 阿里云K8s实战手册 [阿里云CDN排坑指南]CDN ECS运维指南 DevOps实践手册 Hadoop大数据实战手册 Knative云原生应用开发指南 OSS 运维实战手册 云原生架构白皮书 Zabbix企业级分布式监控系统源码文档 Linux&Python自学资料包 10G面试题戳领