在/etc/logrotate.d/目录下创建一个配置文件”nginx”,内容如下:
#vim /etc/logrotate.d/nginx/usr/local/nginx/logs/*.log {dailyrotate 5missingokdateextcompressnotifemptysharedscriptspostrotate [ -e /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` endscript}
注释:
/usr/local/nginx/logs/*.log:需要轮询日志路径daily:每天轮询rotate 5:保留最多5次滚动的日志missingok:如果日志丢失,不报错继续滚动下一个日志dateext:使用日期作为命名格式compress:通过gzip压缩转储以后的日志notifempty:当日志为空时不进行滚动sharedscripts:
/var/run/nginx.pid: nginx pid位置,请查看nginx.conf
postrotate/endscript:在截断转储以后需要执行的命令立即截断可执行下面
/usr/sbin/logrotate -f /etc/logrotate.d/nginx
注:
由于logratate已经加到cron.daily(/etc/cron.daily/logrotate),不再需要加到计划任务中