How to Rotate Nginx Logs with Shell Scripts and Logrotate
This guide explains why nginx logs need regular rotation, shows how to manually split logs using a shell script that backs up, renames, and signals nginx to reopen log files, and demonstrates configuring and testing the logrotate utility for automated daily log rotation and compression.
Why rotate nginx logs?
By default nginx writes continuously to a single access.log and error.log, which can grow huge over time, making processing difficult. Regular rotation—typically daily—keeps file sizes manageable.
Manual rotation with a shell script
Steps:
Check current log files ( ls /var/log/nginx).
Rename the current log to a dated backup (
mv /var/log/nginx/access.log /var/log/nginx/access.log.$(date +%F)).
Send the USR1 signal to the nginx master process so it reopens a fresh log file (
kill -USR1 $(ps -ef | grep nginx | grep master | awk '{print $2}')).
Verify that a new access.log appears and that the old file is archived.
A complete script ( cut_log.sh) automates these steps:
#!/bin/bash
logs_path="/var/log/nginx"
back_logs_path="${logs_path}/$(date -d 'yesterday' +'%F')"
mkdir -p "${back_logs_path}"
cd "${logs_path}" && find . -type f | xargs -i mv {} {}.$(date -d 'yesterday' +'%F')
cd "${logs_path}" && find . -type f | xargs -i mv {} "${back_logs_path}"
kill -USR1 `cat /var/run/nginx.pid`Scheduling the script
Add a daily cron entry, e.g. 0 0 * * * /bin/bash /etc/nginx/cut_log.sh, to run the script automatically each night.
Using logrotate
logrotate is a standard tool that can rotate and compress logs automatically. The default nginx configuration file /etc/logrotate.d/nginx contains:
/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
}Key directives:
daily – rotate each day.
rotate 52 – keep up to 52 archives.
compress and delaycompress – compress after the next rotation.
postrotate – signals nginx to start a new log file.
To test, generate traffic with ab -c 100 -n 10000 http://10.0.0.9/, then force a rotation with logrotate -f /etc/logrotate.d/nginx. The command creates dated log files and, after the next run, compressed .gz archives.
Full workflow
1. Install httpd-tools for ab. 2. Generate load to fill the logs. 3. Run the manual script or let logrotate handle rotation. 4. Verify rotated files with ls -l /var/log/nginx. 5. Ensure the cron job is active.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
