0
0
Nginxdevops~10 mins

Log rotation in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Log rotation
Log file grows
Check size/time limit
Yes
Rotate log: rename old
Create new empty log
Reload nginx to use new log
Continue logging
Log rotation checks if the log file is too big or old, then renames it and creates a new log file, finally nginx reloads to write to the new log.
Execution Sample
Nginx
logrotate /etc/logrotate.d/nginx
systemctl reload nginx
This runs logrotate on nginx logs and reloads nginx to start logging to new files.
Process Table
StepActionCondition/CheckResultSystem State Change
1Check current nginx log file sizeIs size > limit?NoLog file continues growing
2Check current nginx log file sizeIs size > limit?YesPrepare to rotate logs
3Rename current log fileN/AOld log renamed with timestampOld log saved
4Create new empty log fileN/ANew log file createdNew log ready for writing
5Reload nginx serviceN/ANginx reloads configurationNginx writes logs to new file
6Continue loggingN/ALogs written to new fileLogging continues smoothly
💡 Log rotation completes after nginx reloads and new log file is active
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
log_file_sizegrowingexceeds limitexceeds limitreset to 0reset to 0growing
log_file_namenginx.lognginx.lognginx.log.1 (renamed)nginx.log (new)nginx.log (new)nginx.log (new)
nginx_statusrunningrunningrunningrunningreloadedrunning
Key Moments - 3 Insights
Why do we reload nginx after rotating logs?
Because nginx keeps the old log file open, reloading makes it close the old file and start writing to the new one, as shown in step 5 of the execution_table.
What happens if we don't create a new empty log file?
Nginx would fail to write logs after rotation because the new log file doesn't exist yet. Step 4 shows creating the new empty log file before reload.
How does logrotate decide when to rotate logs?
It checks conditions like file size or time interval. Step 2 shows checking if the log file size exceeds the limit to trigger rotation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the system state after step 3?
ANginx service reloaded
BOld log file renamed with timestamp
CNew empty log file created
DLog file size reset to zero
💡 Hint
Check the 'Result' and 'System State Change' columns in step 3
At which step does nginx start writing logs to the new file?
AStep 5
BStep 4
CStep 6
DStep 3
💡 Hint
Look at the 'System State Change' column describing logging continuation
If the log file never exceeds the size limit, which step will never happen?
AStep 1
BStep 2
CStep 3
DStep 6
💡 Hint
Step 3 happens only if the condition in step 2 is Yes
Concept Snapshot
Log rotation renames old logs and creates new ones to prevent huge files.
It checks size or time limits to decide when to rotate.
After rotation, nginx must reload to write to new logs.
Commands: logrotate config + systemctl reload nginx.
This keeps logs manageable and nginx logging continuous.
Full Transcript
Log rotation in nginx works by monitoring the log file size or age. When the log file grows too large or reaches a time limit, the system renames the current log file to save it as an old log with a timestamp. Then, it creates a new empty log file for nginx to write fresh logs. To make nginx start writing to the new log file, the nginx service is reloaded. This process prevents log files from becoming too big and keeps logging smooth. The key steps are checking the log size, renaming the old log, creating a new log, and reloading nginx. Without reloading, nginx would continue writing to the old file. This cycle repeats as logs grow over time.