设置nginx日志滚动
需求:设置nginx每天凌晨12点轮转,系统版本为debian7,nginx版本为tengine2.2.0
1、修改logrotate主配置文件,打开压缩和以时间为后缀命名
# vim /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
# packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
missingok
monthly
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0660 root utmp
rotate 1
}
# system-specific logs may be configured here
2、添加一条nginx的logrotate配置(正常配置到这里就可以了)
# vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
daily
rotate 7
compress
missingok
sharedscripts
postrotate
if [ -f /usr/local/nginx/sbin/nginx-tengine.pid ]; then
kill -USR1 `cat /usr/local/nginx/sbin/nginx-tengine.pid`
fi
endscript
}
3、删除系统自带的每日轮转任务
# mv /etc/cron.daily/logrotate /tmp/
4、添加一条计划任务,每天凌晨12点轮转
# crontab -e
0 0 * * * /usr/sbin/logrotate /etc/logrotate.conf
5、修改crontab时区,避免时区问题
# vim /etc/crontab
CRON_TZ=Asia/Shanghai
# /etc/init.d/cron restart
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步