What if your server suddenly stops because logs filled up the disk? Log rotation saves you from that nightmare.
Why Log rotation in Nginx? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a busy website using nginx. Every day, your server creates huge log files recording every visitor and error. Without managing these logs, they keep growing and take up all your disk space.
Manually checking and deleting or moving log files is slow and easy to forget. If logs grow too big, your server can slow down or crash. Also, important information might get lost if you delete logs carelessly.
Log rotation automatically renames and compresses old log files and starts fresh ones. This keeps logs organized, saves disk space, and ensures your server runs smoothly without manual work.
rm /var/log/nginx/access.log
mv /var/log/nginx/access.log.1 /backup/logrotate /etc/logrotate.d/nginx
Log rotation lets your server handle logs efficiently, so you never run out of space and always keep important data safe.
A busy online store uses log rotation to keep daily sales logs manageable and quickly find issues without worrying about disk space running out.
Manual log management is slow and risky.
Log rotation automates organizing and compressing logs.
This keeps servers stable and logs accessible.
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
