Logrotate (IV)
Nginx
/etc/logrotate.d/nginx:
-----------------------
/var/log/nginx/*.log {
...
rotate 14 <== Give an appropriate value
...
}
/var/log/nginx/*/*.log {
daily
missingok
rotate 14 <== Give an appropriate value
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
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
}
The first entry (/var/log/nginx/*.log
) will rotate the nginx general logs.
The second entry (/var/log/nginx/*/*.log
) will rotate the logs for every web application. If we are to deploy several applications in the same server, and we require different configurations for them (e.g to copy the logs to another host), we must create different entries. One for each application.
To check that logrotate
is going to rotate the logs that we want, we can run the following command:
_$: logrotate --debug /etc/logrotate.d/nginx
Note: If we enable logrotate
on day D
[flashback to Normandy], on day D+1
logrotate
will mark that file as rotated for the last time on day D+1
. On day D+2
, assuming a daily rotation, the file will be rotated. On day D+6
rest from all of your work.
To check when a log file was rotated for the last time:
_$: cat /var/lib/logrotate/status
Supervisor
/etc/logrotate.d/supervisor:
----------------------------
/var/log/supervisor/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 root root
postrotate
kill -USR2 `cat /run/supervisord.pid`
supervisorctl restart all
endscript
}
Example: Copy the logs to another host
/etc/logrotate.d/nginx-webapp:
------------------------------
/var/log/nginx/webapp-com/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi; \
scp /var/log/nginx/webapp-com/* \
backupuser@devops.example.com:/srv/backup/logs/webapp/web
endscript
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
endscript
}
_$: ssh-copy-id -i .ssh/id_rsa.pub backupuser@devops.example.com