20

I'm hoping to get some direction on how to set up truncating and gzip'ing on my domains' access logs. I notice that the core nginx access logs get split and compressed by default, yet my individual access logs continue to grow.

Is this something that can be set up and is handled by nginx or something else on my system that is managing it's core logs?

Draculater
  • 313
  • 2
  • 3
  • 9

1 Answers1

35

/etc/logrotate.d/nginx

/var/log/nginx/access_log {
    rotate 7
    size 5k
    dateext
    dateformat -%Y-%m-%d
    missingok
    compress
    sharedscripts
    postrotate
        test -r /var/run/nginx.pid && kill -USR1 `cat /var/run/nginx.pid`
    endscript
}
quanta
  • 50,327
  • 19
  • 152
  • 213
  • 1
    Awesome. Logrotate was new to me. Makes perfect sense now. Thanks! – Draculater Sep 13 '12 at 15:41
  • `man logrotate` for additional details. – Seth Apr 03 '17 at 18:31
  • 1
    I had to do the following to make it start with new config: `sudo logrotate -v -f /etc/logrotate.d/nginx` – talsibony Mar 22 '18 at 10:54
  • 3
    The command that actually rotates the logs is "kill -USR1 /var/run/nginx.pid". This does not kill the Nginx process, but instead sends it a signal causing it to reload its log files. This will cause new requests to be logged to the refreshed log file. [Source](https://www.digitalocean.com/community/tutorials/how-to-configure-logging-and-log-rotation-in-nginx-on-an-ubuntu-vps) – Edson Horacio Junior Aug 09 '18 at 14:30
  • 1
    Could somebody explain what these are and what this is doing? – Esqarrouth Apr 03 '19 at 22:51
  • From man page: "Normally, logrotate is run as a daily cron job". It doesn't require running it with -v or -f etc. like in talsibony's comment. More details in https://unix.stackexchange.com/questions/116136/how-to-make-log-rotate-change-take-effect/116138 – Darko Maksimovic May 07 '19 at 09:59