Back

使用logrotate 为nginx日志 分卷 (rotate the nginx logs )

发布时间: 2013-06-14 00:41:00

see:  http://serverfault.com/questions/284729/nginx-log-rotation

1. locate your Nginx pid file:   ( normally it's at:  /opt/nginx/logs/nginx.pid  in my Ubuntu)

2. you should know this key process:  

# this just simply make nginx start a new log file, but NOT restart.  ( quite fast)
kill -USR1 `cat /opt/nginx/logs/nginx.pid`

3. create a new file (/etc/logrotate.d/nginx)  containing this : 

# nginx SIGUSR1: Re-opens the log files.
"/opt/nginx/logs/access.log" "/opt/nginx/logs/error.log"{
  daily
  rotate 7
  dateext
  copytruncate
  missingok
  notifempty
  delaycompress
  sharedscripts
  postrotate
    test ! -f /opt/nginx/logs/nginx.pid || kill -USR1 `cat /opt/nginx/logs/nginx.pid`
  endscript
}

4. (注意: 对于 18年以后的ubuntu, 这个文件是 /var/lib/logrotate/status ,下面都是一样)  记住: logrotate 运行时,是有一个自己的状态记录文件的。( /var/lib/logrotate.status ) 它会在这个文件中记录好每个文件的最早时间。然后每次运行时把目标文件跟自己的表做对比。如果发现目标文件的日期超过了这个表的日期,比如一个星期,那么 logrotate 才会对它分卷。  ( keep in mind that logrotate has its own status file which is used to remember when the target file is created and determine if the target file should be rotated.  ) 

5. 典型命令:  $logrotate -v /etc/logrotate.d/nginx   (如果你使用了 -vd 那么就仅仅是一个dry-run 预演,不会对 /var/lib/logrotate.status 生效 ) ( also remember that don't use the debug mode since it's just lead to a dry-run result and won't take effect on /var/lib/logrotate.status file ) 

6. 把logrotate增加到你的 crontab中去。  (make it affect via crontab -e) 

例如:
# in crontab editor: 
0 0 * * * logrotate -v /etc/logrotate.conf

p.s. 所以,想要你的改动立即生效的话,

1. 先运行logrotate : $  logrotate -v /etc/logrotate.d/nginx

2. 编辑 vim /var/lib/logrotate.status 把里面对应的内容,改成昨天。

例如:"/usr/local/nginx/logs/access.log" 2013-12-22 

 要改成: "/usr/local/nginx/logs/access.log" 2013-12-21 

3. 再运行: logrotate -v /etc/logrotate.d/nginx

Back