Docker containers can generate a large amount of log data over time. If you're running Nginx as a reverse proxy for multiple services, access and error logs can quickly grow to consume several gigabytes of disk space.
Fortunately, you don't need a complicated logging stack to keep things under control. Linux already provides a simple and reliable solution in
logrotate.
In this guide, we'll configure logrotate to automatically rotate Nginx logs stored on the host, compress older logs, and retain the last 14 rotations. We'll also make sure Nginx reopens its log files after each rotation, without restarting the container.
The same approach can be adapted to other Docker containers and services that write their logs directly to the host filesystem.
1. Create a Logrotate Configuration
First, create a dedicated configuration file for your Nginx Docker logs:
sudo nano /etc/logrotate.d/nginx-docker
Add the following configuration:
/var/log/nginx/*.log {
daily
rotate 14
missingok
notifempty
compress
dateext
dateformat -%Y%m%d
create 0644 root root
sharedscripts
postrotate
/usr/bin/docker kill --signal=USR1 nginx-proxy >/dev/null 2>&1 || true
endscript
}
Save the file and exit.
Understanding the Configuration
Here's what each directive does:
/var/log/nginx/*.log - Matches all log files ending in .log in the specified directory.
daily - Rotates logs daily.
rotate 14 - Retains the 14 most recent rotated copies of each log.
missingok - Doesn't report an error if a matching log file is missing.
notifempty - Skips rotation of empty log files.
compress - Compresses rotated logs using gzip to reduce disk usage.
dateext - Uses dates instead of sequential numbers in rotated filenames.
dateformat -%Y%m%d - Formats the date as YYYYMMDD, producing filenames such as access.log-20261009.gz.
create 0644 root root - Creates a replacement log file with permissions 0644 and ownership root:root.
sharedscripts - Executes the post-rotation script once for the entire group of matching logs, rather than once per file.
Why Send Nginx a USR1 Signal?
Nginx keeps its log files open while running. Simply renaming a log file during rotation doesn't make Nginx start writing to the newly created file. It will continue writing to the original file descriptor, which now points to the rotated archive.
To prevent this, we send Nginx a
USR1 signal:
docker kill --signal=USR1 nginx-proxy
Nginx responds by reopening its log files, allowing it to continue writing to the newly created files.
Notice that we're targeting the Docker container named
nginx-proxy. Docker forwards the signal to the container's main process, and Nginx handles the signal internally.
There's no need to restart the container or interrupt existing connections.
The
|| true at the end prevents logrotate from treating a failed Docker command as a fatal error. This is useful if the container happens to be stopped during rotation.
What About Multiple Services?
If you're using Nginx as a reverse proxy for several services, you might have log files such as:
bestservice.access.log
bestservice.error.log
servicebutokay.access.log
servicebutokay.error.log
myGGservice.access.log
myGGservice.error.log
The wildcard automatically includes all matching
.log files. You don't need a separate configuration for each service.
2. Test the Configuration
Before allowing logrotate to handle your logs automatically, it's a good idea to verify that the configuration is valid.
Start with a debug run:
sudo logrotate -d /etc/logrotate.d/nginx-docker
The
-d option runs logrotate in debug mode. It checks the configuration and reports what it would do without actually modifying any files.
Look for syntax errors, warnings, or unexpected behaviour.
Force a Rotation
If everything looks good, you can perform an actual rotation to verify the complete process:
sudo logrotate -vf /etc/logrotate.d/nginx-docker
The options mean:
-v - Verbose output, showing what logrotate is doing.
-f - Forces rotation regardless of whether the normal rotation conditions have been met.
Important: Unlike the debug run, this command actually rotates your logs. Existing log files will be renamed, compressed, and replaced with new empty files.
If you already have gigabytes of accumulated logs, this is a convenient way to reclaim disk space immediately rather than waiting for the next scheduled rotation.
It also executes the postrotate script, sending the USR1 signal to Nginx so it reopens its log files.
3. Verify the Results
Once the rotation has completed, inspect your Nginx log directory:
sudo ls -lh /var/log/nginx/
You should now see your active log files alongside their compressed archives.
For example:
access.log
access.log-20261009.gz
error.log
error.log-20261009.gz
bestservice.access.log
bestservice.access.log-20261009.gz
servicebutokay.access.log
servicebutokay.access.log-20261009.gz
The dates and filenames will naturally depend on your actual log files and when you run the rotation.
Check That Nginx Is Still Running
Verify that the Nginx container is still running:
sudo docker ps --filter name=nginx-proxy
You should see the container listed as running.
Next, check that the new log files are receiving requests:
sudo tail -n 10 /var/log/nginx/access.log
You should see recent requests appearing as Nginx continues writing to the new log files.
4. Automatic Rotation and Retention
You don't need to schedule anything manually.
On a standard Ubuntu installation, logrotate is normally executed automatically through a systemd timer. The system takes care of checking the configured rotation rules and processing eligible logs.
You can check the timer with:
systemctl status logrotate.timer
You can also inspect the last execution:
systemctl status logrotate.service
Since we forced a rotation during testing, logrotate will also have updated its state file to record the rotation.
With
daily and
rotate 14, each log retains up to 14 rotated copies. In practice, this gives you approximately two weeks of historical logs, depending on when the files were last rotated.
One thing worth remembering is that retention is handled independently for each log file. The 14-copy limit applies to every matching file, not to the directory as a whole.
Also, logrotate doesn't immediately delete every existing archive when you introduce a new configuration. Older files that aren't managed by this rule will need to be handled separately if you want to reclaim all their disk space.
Comments (0)
No comments yet. Be the first to comment!
Leave a Comment