Implementing logrotate for nginx access and error logs.

To check for your existing nginx logs, go to :

# cd /var/log/nginx

Move all logs to /backup/anyname.com/log

#cd /etc/logrotate.d

sudo nano /etc/logrotate.d/anyname.com

  1 /var/log/nginx/anyname.com/*access_log
  2 /var/log/nginx/anyname.com/*error_log
  3 {
  4         daily
  5         missingok
  6         rotate 10
  7         dateext
  8         compress
  9         delaycompress
 10         create 644 dev root
 11         sharedscripts
 12         lastaction
 13          cp /var/log/nginx/anyname.com/*.gz /backup/anyname.com/log
 14          rm /var/log/nginx/anyname.com/*.gz
 15          systemctl restart nginx
 16         endscript
 17 }
logrotate in cron.daily was created by DEFAULT
(Inside values are by default, no added line)
  1 #!/bin/sh
  2
  3 # Clean non existent log file entries from status file
  4 cd /var/lib/logrotate
  5 test -e status || touch status
  6 head -1 status > status.clean
  7 sed 's/"//g' status | while read logfile date
  8 do
  9     [ -e "$logfile" ] && echo "\"$logfile\" $date"
 10 done >> status.clean
 11 mv status.clean status
 12
 13 test -x /usr/sbin/logrotate || exit 0
 14 /usr/sbin/logrotate /etc/logrotate.conf

DONE!

Logrotation will be done daily.
Then after, will be moved to /backup

Leave a comment