How to Prevent Nginx Log Overgrowth with Automated Log Splitting
This guide explains why default Nginx logging can fill disks, then walks through disabling logging, clearing old files, re‑enabling logging, using a shell script to rotate logs by date, and scheduling the script with cron to keep server storage healthy.
When a project goes live with default Nginx settings, logs are written to ../nginx/logs/access.log and grow without rotation, eventually exhausting disk space.
This article presents a step‑by‑step solution to clean up and split Nginx logs.
1. Close file handles
Delete old logs, then disable logging in nginx.conf by setting access_log off and restart Nginx.
Check that no process holds the file:
lsof access.log2. Empty the current log
Run: echo "" > access.log This command clears the file quickly without affecting system performance.
3. Re‑enable logging
Comment out the access_log line in nginx.conf and restart Nginx.
4. Run the log‑splitting script
Give execution permission: chmod 774 nginx_log_split.sh Execute the script: sh nginx_log_split.sh The script creates a history_log directory, moves the current access.log (and error.log) to a dated file, and reopens Nginx logs.
5. Schedule the script with cron
Add a daily job (23:59) to run the script and redirect output:
59 23 * * * /home/user/nginx/logs/nginx_log_split.sh > /home/user/nginx/logs/history_log/execute.log 2>&1After these steps the Nginx logs are automatically split, preventing disk‑space exhaustion.
Lin is Dream
Sharing Java developer knowledge, practical articles, and continuous insights into computer engineering.
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.
