Tidy up /var/log/nginx easily

When logs take too much space and prevent you from listing and debugging easily in /var/log/nginx, it's time to act.

logrotate.d i generally installed by defaults on distros like Debian and Ubuntu and can help us in this task.

Nginx being a good guy, it already has a rotation script ready for us :

/etc/logrotate.d/nginx

Attention : logrotate must be installed before nginx if you want nginx to write this defautl script

... this script is well-thought and has some useful directives for us :

/var/log/nginx/*.log {
    daily
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    olddir /var/log/nginx/archive
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
           run-parts /etc/logrotate.d/httpd-prerotate; \
        fi; \
    endscript
    postrotate
        [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

Some details

  • daily at the top indicates the rotation frequency
  • the olddir directive indicates in which folder are going to be archived the old logs (that's to say, old for each rotation period; for daily it will be up to the day before)
  • delaycompress allows to let the last log untarred.