暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Nginx日志切割的方法

王佐的运维笔记 2020-05-30
524

在日常的工作当中,日志是最直观的分析事件的手段,日志文件包含了关于系统中发生事件的有用信息,在排查故障的过程或者系统性能分析时常常被用到。但,对于忙于的服务器,日志文件的大小会增长很快,服务器的磁盘空间会很快被消耗殆尽,这就成为了一个问题。

除此之外,处理一个单个的庞大日志文件也常常是件十分棘手的事。Nginx日志切割方式有很多种,本章节拿出两种案例来进行分享,分别为 Logrotate 工具切割和脚本切割。


Logrotate切割Nginx日志

切割方式一


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


#配置详解:
# daily 按天切割
# rotate 35保留35个日志文件
# missingok 切割中遇到日志错误忽略
# notifempty 日志如果为空将不进行切割和压缩
# compress 压缩
# nodelaycompress 不要将刚切割后的日志文件放到下个循环中进行压缩
# copytruncate 复制源日志文件后并清空源日志文件
# dateext 切割后的文件添加日志扩展名
# dateformat -%Y-%m-%d
# dateyesterday 切割后的文件日志命名为昨天的日期


# 测试
1.写入100M内容
[root@nginx01 logs]# head -c 100M < /dev/urandom > /opt/nginx/logs/error.log
[root@nginx01 logs]# head -c 100M < /dev/urandom > /opt/nginx/logs/access.log


2.强制执行切割(不管是否到了任务计划自动执行时间)
[root@nginx01 logs]# logrotate -vf /etc/logrotate.d/cut_nginx
-v 显示切割过程中的详细信息
-f 强制执行切割
3.查看切割效果
[root@nginx01 logs]# ll
total 201M
-rw-r--r-- 1 nginx root 100M May 20 09:47 error.log-2020-05-30.gz
-rw-r--r-- 1 nginx root 101M May 20 09:36 access.log-2020-05-30.gz
-rw-r--r-- 1 nginx root 0 May 20 09:36 access.log
-rw-r--r-- 1 nginx root 0 May 20 09:36 error.log
切割方式二
[root@nginx02 /root]# vim /etc/logrotate.d/cut_nginx
#向Nginx发送信号重新打开日志文件切割
/opt/nginx/logs/*.log {
daily
        rotate 35
missingok
notifempty
compress
nodelaycompress
dateext
dateformat -%Y-%m-%d
dateyesterday
postrotate
if [ -f /opt/nginx/run/nginx.pid ];then
kill -USR1 `cat /opt/nginx/run/nginx.pid`
fi
endscript
}
#配置详解:
# daily 按天切割
# rotate 35保留35个日志文件
# missingok 切割中遇到日志错误忽略
# notifempty 日志如果为空将不进行切割和压缩
# compress 压缩
# nodelaycompress 不要将刚切割后的日志文件放到下个循环中进行压缩


# dateext 切割后的文件添加日志扩展名
# dateformat -%Y-%m-%d
# dateyesterday 切割后的文件日志命名为昨天的日期
# 最后  在切割后执行 postrotate / endscript 之间的命令,向nginx发送信号重新打开日志
切割方式三
#不同格式文件大小切割
"/opt/nginx/logs/access.log" "/opt/nginx/logs/error.log"  {
        size 200M
        rotate 35
missingok
notifempty
compress
nodelaycompress
copytruncate
dateext
dateformat -%Y-%m-%d-%H
}


#配置详解:
# size 按大小切割
# rotate 35保留35个日志文件
# missingok 切割中遇到日志错误忽略
# notifempty 日志如果为空将不进行切割和压缩
# compress 压缩
# nodelaycompress 不要将刚切割后的日志文件放到下个循环中进行压缩
# dateext 切割后的文件添加日志扩展名
# dateformat -%Y-%m-%d






切割时间 上面讲logrotate按时间切割有 天|周|月|,那么具体时间是几点钟呢?logrotate默认的定时任务放在了 /etc/cron.daily/logrotate 文件中
[root@nginx01 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@nginx01 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的时间s 可以修改的,如:START_HOURS_RANGE= 24-01 (这是日志切割时间点,即只在24点到1点开始切割,不出意外就是24点)
#自己写crontab 但是 要把 logrotate 的任务计划给删掉 然后在每天凌晨00:00进行切割nginx日志


# 个人习惯,一般更改时  做备份
[root@nginx01 logs]# mv  /etc/cron.daily/logrotate /etc/cron.daily/logrotate.bak
[root@nginx01 logs]# crontab -e
#cut_nginx_logs
00 00 * * * /sbin/logrotate -f /etc/logrotate.d/nginx

这样就可以使用了 。大家可以自己尝试下
文章转载自王佐的运维笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论