Nginx log rotation example config

logrotate -- is an utility that helps automate old logs processing.

Exampole below is rotation of nginx logs.

With root privileges, create file in directory /etc/logrotate.d/ , for example /etc/logrotate.d/my:

/path/to/logs/*.log {
    rotate 5
    create 644 nginx root
    dateformat -%Y-%m
        /bin/kill -USR1 `cat /run/ 2>/dev/null` 2>/dev/null || true


  1. /path/to/logs/*.log change to the directory with your nginx logs
  2. create 644 nginx root -- here nginx root is name and group of created log files. Copy these from your actual nginx logs.

Launch with flag -d (debug -- do nothing, just test) and -f (activate rotation despite calendar)

logrotate -d -f /etc/logrotate.d/my

Expect info about wich logs will be copied and where. If everything is ok, you can log out and wait for packed logs at the first day of next month.

Lets look at config row by row:

/path/to/logs/*.log { # mask to find logs
    monthly  # rotate at first date of every month
    notifempty # skip empty logs
    missingok  # do not treat empty log as error
    rotate 5 # keep 6 logs in rotation, for every log where will be 5 files for 5 previous months
    compress # compress rotated logs to .gz
    create 644 nginx root # create empty log file
    dateext # add date to archive file name
    dateformat -%Y-%m # set date format as 2019-04 (April of 2019)
        # kick nginx so it start to write logs into new file
        /bin/kill -USR1 `cat /run/ 2>/dev/null` 2>/dev/null || true 

Additional reading: rtfm

published 2019-04-03 13dagger