Nginx日志按天分割

时间:2021-01-13 23:00:16

核心思想:使用crontab在每日23:59执行日志分割。

1、配置nginx日志信息,vim /etc/logrotate.d/nginx

/var/log/nginx/*.log {
nocompress
daily
copytruncate
create
notifempty
rotate 7
olddir /var/log/nginx
missingok
dateext
postrotate
/bin/kill -HUP `cat /var/run/nginx.pid 2> /dev/null` 2> /dev/null || true
endscript
}

2、配置crontab,vim /etc/crontab或者执行crontab -e添加如下信息:

  * * * root ( /usr/sbin/logrotate -f /etc/logrotate.d/nginx)

3、重启cron

service cron restart