nginx の logrotate

「ローテーションするスクリプト書いて、cronで回す」か、「logrotateに任せる」かのどちらか。

スクリプト各場合は以下の通り。


$ mv access.log access.log.0
$ kill -USR1 cat master.nginx.pid
$ sleep 1
$ gzip access.log.0 # do something with access.log.0

via http://wiki.nginx.org/NginxLogRotation

logrotateを使う場合は、/etc/logrotate.d に以下の設定を追加。
postrotate の2行目はファイルの日付がrotateした日になるので、無理矢理前日日付に変更しているだけです。


/var/log/httpd/access.log {
daily
dateext
rotate 365
missingok
ifempty
sharedscripts
postrotate
test ! -f /var/run/nginx.pid || kill -USR1 `cat /var/run/nginx.pid`
/bin/mv /var/log/httpd/access.log-`date '+%Y%m%d'` /var/log/httpd/access.log.`date '+%Y%m%d' -d '1days ago'`
endscript
}