How to Automate Nginx Log Rotation with Shell Scripts and Logrotate
This guide explains why nginx logs need regular rotation, demonstrates manual log backup and rotation using shell commands, provides a complete Bash script for daily log splitting, and shows how to configure and use the logrotate tool for automated, compressed log management.
Log Rotation (Shell Script)
nginx logs are not rotated by default, so long‑running sites accumulate huge log files; therefore logs are usually rotated daily.
Manual log rotation and backup
-rw-r--r-- 1 root root 2.1G May 8 13:57 front_access.log1. Send a signal to the nginx process to make it create a new log file (the basic log rotation step).
/var/log/nginx/access.log.bak # backup original log file
nginx reload # send reopen signal
# The log file defined in the configuration is /var/log/nginx/access.log
# After the signal, nginx creates a new file at the same path for new entries.2. Generate a large amount of traffic to fill the log (e.g., using a loop or ab).
# for num in {1..10000}; do curl 10.0.0.8; done ab -c 100 -n 10000 http://10.0.0.8/3. Check the current number of log lines. cat all-server-accesss.log | wc -l # 20000 4. Rotate the log: rename the current file with a date suffix, then send the USR1 signal to nginx to reopen a fresh log.
cd /var/log/nginx && mv all-server-accesss.log all-server-accesss.log.$(date '+%F')
kill -USR1 $(ps -ef | grep nginx | grep master | awk '{print $2}')
# New log file appears and receives further entries.Shell script for automated rotation
#!/bin/bash
# Source log directory
logs_path="/var/log/nginx"
# Backup directory (yesterday's date)
back_logs_path="${logs_path}/$(date -d 'yesterday' +'%F')"
mkdir -p "${back_logs_path}"
# Rename old logs with date suffix
cd "${logs_path}" && find . -type f | xargs -I {} mv {} {}.$(date -d 'yesterday' +'%F')
# Move renamed logs to backup directory
cd "${logs_path}" && find . -type f | xargs -I {} mv {} "${back_logs_path}"
# Tell nginx to reopen logs
kill -USR1 `cat /var/run/nginx.pid`Testing the script
1. List current logs, then rename them with rename to add a date suffix.
ls
rename log log.$(date +%F) *.log2. Send the USR1 signal to nginx.
nginx -s reopen # or kill -USR1 $(cat /var/run/nginx.pid)3. Verify that new log files are created.
Logrotate configuration
logrotate can automate daily rotation, compression, and retention.
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}Run logrotate -f /etc/logrotate.d/nginx to force rotation, then add a cron job:
0 0 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx >> /var/log/nginx/logrotate_nginx.log 2>&1The above steps provide both a manual and automated solution for splitting and compressing nginx logs on a daily basis.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
