0
0
Nginxdevops~5 mins

Log rotation in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Log rotation helps keep log files from growing too large and using too much disk space. It automatically renames old logs and creates new ones so your server runs smoothly without manual cleanup.
When your nginx access or error logs grow very large and slow down your server.
When you want to keep logs organized by date for easier troubleshooting.
When disk space is limited and you need to delete old logs automatically.
When you want to compress old logs to save storage.
When you want to keep a fixed number of old log files for audit purposes.
Config File - logrotate.conf
logrotate.conf
/var/log/nginx/*.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        systemctl reload nginx > /dev/null 2>&1 || true
    endscript
}

This configuration tells the logrotate tool to manage all nginx log files in /var/log/nginx/.

daily: rotate logs every day.

missingok: don't error if logs are missing.

rotate 7: keep 7 old logs before deleting.

compress: compress old logs to save space.

delaycompress: delay compression until the next rotation.

notifempty: skip rotation if log is empty.

create 0640 www-data adm: create new log files with permissions and ownership.

sharedscripts: run postrotate script only once.

postrotate: reload nginx to start writing to new logs.

Commands
This command runs logrotate in debug mode to check the configuration without making changes. It helps verify your log rotation setup.
Terminal
sudo logrotate -d /etc/logrotate.conf
Expected OutputExpected
/var/log/nginx/*.log: log does not need rotating.
-d - Debug mode: shows what would happen without changing files
This forces logrotate to rotate the logs immediately according to the configuration. Useful to test or apply rotation right away.
Terminal
sudo logrotate -f /etc/logrotate.conf
Expected OutputExpected
No output (command runs silently)
-f - Force rotation even if not needed
List the nginx log files to see rotated and compressed logs after rotation.
Terminal
ls -l /var/log/nginx/
Expected OutputExpected
total 24 -rw-r----- 1 www-data adm 1234 Apr 26 10:00 access.log -rw-r----- 1 www-data adm 5678 Apr 25 10:00 access.log.1 -rw-r----- 1 www-data adm 3456 Apr 24 10:00 access.log.2.gz -rw-r----- 1 www-data adm 7890 Apr 26 10:00 error.log -rw-r----- 1 www-data adm 2345 Apr 25 10:00 error.log.1 -rw-r----- 1 www-data adm 1234 Apr 24 10:00 error.log.2.gz
Key Concept

If you remember nothing else from log rotation, remember: rotating and compressing logs regularly keeps your server fast and your disk clean.

Common Mistakes
Not reloading nginx after log rotation
Nginx keeps writing to old log files if not reloaded, so new logs won't be created.
Use a postrotate script to reload nginx after rotating logs.
Setting rotate count too low or too high
Too low deletes logs too soon; too high wastes disk space.
Choose a rotate count that balances disk space and log retention needs, like 7 days.
Not compressing old logs
Old logs take up unnecessary disk space if not compressed.
Enable compression with the compress and delaycompress options.
Summary
Create a logrotate configuration file to manage nginx logs automatically.
Use 'logrotate -d' to test the configuration without changes.
Use 'logrotate -f' to force immediate log rotation.
Reload nginx after rotation to start new log files.
Check rotated and compressed logs in the log directory.