How to limit nginx access log file size and compress?

29,460

/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
}
Share:
29,460

Related videos on Youtube

Draculater
Author by

Draculater

Updated on September 18, 2022

Comments

  • Draculater
    Draculater over 1 year

    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?

    • Michael Hampton
      Michael Hampton over 11 years
      Use logrotate?
    • ABS
      ABS over 3 years
      Here's a good detailed post about using logrotate for nginx
  • Draculater
    Draculater over 11 years
    Awesome. Logrotate was new to me. Makes perfect sense now. Thanks!
  • Seth
    Seth about 7 years
    man logrotate for additional details.
  • talsibony
    talsibony about 6 years
    I had to do the following to make it start with new config: sudo logrotate -v -f /etc/logrotate.d/nginx
  • Edson Horacio Junior
    Edson Horacio Junior almost 6 years
    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
  • Esqarrouth
    Esqarrouth about 5 years
    Could somebody explain what these are and what this is doing?
  • Darko Maksimovic
    Darko Maksimovic about 5 years
    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 unix.stackexchange.com/questions/116136/…