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
}
The configuration parameters are:
weekly
: This parameter sets Nginx logs to be rotated weekly. This is usually done at the beginning of the week.missingok
: This parameter forces Logrotate to display no error when a log file is missing.rotate 52
: This parameter sets that the log files should be rotated 52 times before they are deleted.compress
: This parameter sets Logrotate to automatically rotate Nginx log files with the default gzip compression.delaycompress
: Thedelaycompress
parameter sets the compression of preceding logs to be delayed before the next rotation.notifempty
: This parameter sets Logrotate not to rotate log files if the files are empty. This will prevent having empty log files.create 0640
: This tells logrotate to create a new file file with the specified permissions after rotating the original file.sharedscripts
: This parameter tells Logrotate to run thepostrotate
only once per Logrotate run instead of running it whenever the log files are rotated.prerotate
: The commands withinprerotate
(fromprerotate
toendscript
) will run before the log files are rotated.postrotate
: The commands withinpostrotate
(frompostrotate
toendscript
) will run after the log files are rotated (and before logs are compressed).