Operations 5 min read

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.

Lin is Dream
Lin is Dream
Lin is Dream
How to Prevent Nginx Log Overgrowth with Automated Log Splitting

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.log

2. 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>&1

After these steps the Nginx logs are automatically split, preventing disk‑space exhaustion.

log rotationserver operations
Lin is Dream
Written by

Lin is Dream

Sharing Java developer knowledge, practical articles, and continuous insights into computer engineering.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.