316 words, 2 min read

By default, Ubuntu server rotates nginx access and error logs using the system logrotate utility. The defaults are fairly conservative: logs are rotated weekly and only four weeks of history are kept. For many production environments this is too short, especially if you need to analyze traffic patterns or troubleshoot issues over a longer time span. Fortunately, extending the retention period is straightforward.

Nginx does not manage its own log rotation. Instead, Ubuntu relies on logrotate, and the nginx configuration file is located at:

/etc/logrotate.d/nginx

This file defines how often logs are rotated and how many old copies are kept.

A typical Ubuntu install will look like this:

/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
endscript
}

In this case, logs are rotated daily and the system keeps 14 copies, meaning only two weeks of logs are retained.

Two directives matter most:

  • daily, weekly, or monthly – how often rotation occurs.
  • rotate N – how many old log files are preserved.

If you want to keep one year of weekly logs:

weekly
rotate 52

If you prefer daily rotation but want six months of history:

daily
rotate 180

Because compression is enabled by default (compress and delaycompress), older log files will be stored as .gz, saving disk space.

Before applying, test the configuration:

sudo logrotate -d /etc/logrotate.d/nginx

The -d flag runs in debug mode without making changes. Once you’re satisfied, force a rotation:

sudo logrotate -f /etc/logrotate.d/nginx

Extending nginx log retention on Ubuntu is just a matter of tweaking the logrotate policy. By increasing the rotate value and adjusting the frequency, you can safely keep months or even years of access logs, all while keeping disk usage under control thanks to compression. This small change can make your server logs far more useful for long-term monitoring, analytics, and auditing.