Tips / Nginx

How to Rotate Nginx logs

Nginx log rotation official documentation have a big lack of information and today I was looking for a way to rotate nginx logs, as my previous search reveled that nginx didn’t had a rotation mechanism enabled by default… but I was wrong, just checked my latest Nginx 1.4.1 setup from RPM and it did had log rotation enabled.

In case your Nginx doesn’t have log rotation enabled, you can do it in this way using this simple script:

Create a file called /etc/logrotate.d/nginx

pico -w  /etc/logrotate.d/nginx

Put this content inside and modify the first line to match your Nginx directory log, it could be:


Once done, your file should look like this:

/var/log/nginx/*.log {
        rotate 52
        create 640 nginx adm
                [ -f /var/run/ ] && kill -USR1 `cat /var/run/`

Wait 24 hours until cron daily runs and check out if you see any .gz file inside your logs directory, if you see some gzipped files, your Nginx rotation is working fine 😀

Popular search terms:

  • nginx logrotate
  • nginx log rotation
  • nginx log rotate
  • logrotate nginx

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Mark

    Interesting, and thanks for the tip 🙂
    Just one question: If you keep your log files in multiple locations, say:
    /srv/www/{site name}/logs/*.log
    Can you use /srv/www/*/logs/*.log as a server line, or do you need to make separate blocks for each server instance? If the latter, is there a workaround to grab all of them at once?

  • admin

    Never done it before, but you can try, it “should” work, but I can not be 100% sure.

    Let me know if it works for you.

  • psv

    I am a bit unsure about concurrency there. What I mean: what if exactly at moment when logrotation was performed, but postrotate hook was not yet performed, there would be appear new incomming request to nginx. Could this be an issue? Like loosing this bit of log information, or even crash? Seems like it will write to “old” file handle, which should be available, but will be discarded after postrotate step.

  • No need to wait 24, can run: logrotate -f /etc/logrotate.d/nginx as long as you don’t use dateext (which you don’t in the example of this blog post).

  • you can just list your directories that you keep nginx logs. I use nginx to proxy Plone, /usr/local/Plone with its builtin load balancer.. and have generic errors go to /var/log/nginx…

    so i’ve got in my logrotate.d/nginx

    /usr/local/Plone/zeocluster/var/log/*.log {

    rest of example below

    just list your directories/vhost log locations.

    run logrotate -f /etc/logrotate.d/nginx and you should see your results