What's the easiest way to rotate nginx log files monthly? What's the easiest way to rotate nginx log files monthly? nginx nginx

What's the easiest way to rotate nginx log files monthly?


I think the following crontab should work:

0   0   1   *   *   /etc/nginx/logrotate.monthly.sh

Where /etc/nginx/logrotate.monthly.sh should have the following content:

find /var/www/logs/ -name "*log" -exec \mv -i {} {}.`sh -c 'date -r $(expr $(date +%s) - 1209600) +%Y-%m'` \; ; \kill -USR1 `cat /var/run/nginx.pid`

The -i option to mv is important to ensure that files don't get overwritten. We get the date for the filename by moving today's date two weeks back (as per « tcsh: print date 2 weeks ago in shell »).


please check also this misc@ thread.

(also keep in mind the caveat documented in the FAQ about privseped apache and the need for a small time window upon the move.)