Log rotation in Nginx - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Log rotation in nginx helps manage log file sizes by periodically creating new files.
We want to understand how the work needed grows as logs get bigger or more frequent.
Analyze the time complexity of this nginx log rotation snippet.
/var/log/nginx/*.log {
rotate 5;
size 100M;
compress;
delaycompress;
notifempty;
create 0640 www-data adm;
}
This config rotates logs when they reach 100MB, keeps 5 backups, compresses old logs, and creates new files with set permissions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking log file size periodically and compressing rotated logs.
- How many times: Size check happens continuously; compression runs once per rotated file (up to 5 files).
As logs grow larger, nginx checks size often but compression only happens on rotated files.
| Input Size (n) | Approx. Operations |
|---|---|
| 10MB | Many size checks, no rotation yet |
| 100MB | Size check triggers 1 rotation and compression |
| 500MB | Multiple rotations, compressions up to 5 files |
Pattern observation: Size checks happen often but compression work grows linearly with number of rotated files.
Time Complexity: O(n)
This means the work to compress rotated logs grows linearly with the number of rotated files.
[X] Wrong: "Log rotation time grows with total log size continuously."
[OK] Correct: Size checks are frequent but cheap; heavy work happens only on rotated files, which are limited.
Understanding how log rotation scales helps you manage system resources well and shows you grasp real-world server maintenance.
"What if we increased the number of rotated files kept from 5 to 50? How would the time complexity change?"
Practice
Solution
Step 1: Understand log file growth
Log files grow continuously as nginx runs, which can fill up disk space.Step 2: Purpose of log rotation
Log rotation splits logs into smaller files and removes or archives old ones to save space.Final Answer:
To keep log files from growing too large and manage disk space -> Option CQuick Check:
Log rotation = Manage log size and disk space [OK]
- Thinking logs are deleted every hour automatically
- Believing log rotation stops logging
- Assuming logs are merged into one file
Solution
Step 1: Identify nginx reload command
The commandnginx -s reloadtells nginx to reload configuration and reopen log files without stopping the service.Step 2: Compare other options
systemctl restart nginxrestarts nginx fully, which is heavier.service nginx stopstops nginx, andnginx --rotate-logsis not a valid command.Final Answer:
nginx -s reload -> Option BQuick Check:
Reload nginx logs = nginx -s reload [OK]
- Using full restart instead of reload
- Stopping nginx instead of reloading
- Using invalid nginx commands
/var/log/nginx/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
nginx -s reload
endscript
}What happens when logrotate runs?
Solution
Step 1: Analyze rotation frequency and retention
Thedailydirective means logs rotate every day.rotate 7keeps 7 old log files.Step 2: Check compression and reload
compresscompresses old logs,delaycompressdelays compression by one cycle.postrotaterunsnginx -s reloadto reopen logs.Final Answer:
Logs rotate daily, keep 7 old compressed files, and nginx reloads after rotation -> Option DQuick Check:
Daily rotate + 7 files + reload nginx = C [OK]
- Confusing daily with weekly rotation
- Ignoring compression directives
- Assuming nginx restarts instead of reloads
Solution
Step 1: Check log file path correctness
If the log file path in the logrotate config does not match actual nginx log locations, rotation won't happen.Step 2: Evaluate other options
Not reloading nginx delays log reopening but rotation still occurs.rotate 0disables rotation but is rare. Missingcompressonly affects compression, not rotation.Final Answer:
The log file path in logrotate config is incorrect -> Option AQuick Check:
Wrong log path = no rotation [OK]
- Assuming missing reload stops rotation
- Thinking compression affects rotation
- Ignoring log file path accuracy
Solution
Step 1: Match size and rotate count requirements
/var/log/nginx/*.log { size 100M rotate 5 compress missingok notifempty sharedscripts postrotate nginx -s reload endscript } usessize 100Mto rotate at 100MB androtate 5to keep 5 backups, matching requirements.Step 2: Check compression and reload commands
/var/log/nginx/*.log { size 100M rotate 5 compress missingok notifempty sharedscripts postrotate nginx -s reload endscript } includescompressandpostrotate nginx -s reloadfor smooth reload. Others use wrong rotate count, timing, or restart instead of reload.Final Answer:
/var/log/nginx/*.log { size 100M rotate 5 compress missingok notifempty sharedscripts postrotate nginx -s reload endscript } -> Option AQuick Check:
Size 100M + rotate 5 + compress + reload nginx = A [OK]
- Using daily or weekly instead of size-based rotation
- Restarting nginx instead of reloading
- Wrong rotate count or missing compress
