Log rotation in Nginx - Commands & Configuration
/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.
sudo logrotate -d /etc/logrotate.conf
-d - Debug mode: shows what would happen without changing filessudo logrotate -f /etc/logrotate.conf
-f - Force rotation even if not neededls -l /var/log/nginx/If you remember nothing else from log rotation, remember: rotating and compressing logs regularly keeps your server fast and your disk clean.
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
Ready to prove it?
Pass the test and this pattern moves unknown
