How to configure Logrotate

Configure Logrotate using its configuration file.

Logrotate has two options of managing log files:

  • The /etc/logrotate.conffile. It contains default configurations and manages log rotation of some logs that are not owned by any system services.

  • The /etc/logrotate.d/directory. It contains the configuration for specific packages installed.

For example, the Logrotate configuration file for Nginx server is located at /etc/logrotate.d/nginx.

To view the rotation management of Nginx log files, view the file:

$ cat /etc/logrotate.d/nginx

The results should be something like:

/var/log/nginx/*.log {
    weekly
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 0640 www-data admin
    sharedscripts
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi \
    endscript
    postrotate
        invoke-rc.d nginx rotate >/dev/null 2>&1
    endscript
}

Logrotate configuration of Nginx

The configuration parameters are:

  1. weekly: This parameter sets Nginx logs to be rotated weekly. This is usually done at the beginning of the week.

  2. missingok: This parameter forces Logrotate to display no error when a log file is missing.

  3. rotate 52: This parameter sets that the log files should be rotated 52 times before they are deleted.

  4. compress: This parameter sets Logrotate to automatically rotate Nginx log files with the default gzip compression.

  5. delaycompress: The delaycompress parameter sets the compression of preceding logs to be delayed before the next rotation.

  6. notifempty: This parameter sets Logrotate not to rotate log files if the files are empty. This will prevent having empty log files.

  7. create 0640: This tells logrotate to create a new file file with the specified permissions after rotating the original file.

  8. sharedscripts: This parameter tells Logrotate to run the postrotate only once per Logrotate run instead of running it whenever the log files are rotated.

  9. prerotate: The commands within prerotate (from prerotate to endscript) will run before the log files are rotated.

  10. postrotate: The commands within postrotate (from postrotate to endscript) will run after the log files are rotated (and before logs are compressed).