Skip to content

Instantly share code, notes, and snippets.

@kanchokanchev
Last active May 8, 2025 07:15
Show Gist options
  • Select an option

  • Save kanchokanchev/33277bed6933b7e878a9065aef7b4734 to your computer and use it in GitHub Desktop.

Select an option

Save kanchokanchev/33277bed6933b7e878a9065aef7b4734 to your computer and use it in GitHub Desktop.
Nginx - Log Rotation & Compression #Nginx_ADMIN #Nginx_Log

Nginx Log Rotation & Compression Setup Guide


Prerequisites

  • Check available disk space
df -h /
  • Verify gzip is installed
which gzip

1. Backup Current Configuration

sudo cp /etc/logrotate.d/nginx /etc/logrotate.d/nginx.bak

2. Edit Logrotate Configuration

sudo nano /etc/logrotate.d/nginx

Replace contents with (log file extensions may vary on different servers; hence revise and edit where needed):

/etc/nginx/log/*.log /etc/nginx/log/*.access /etc/nginx/log/*.error /var/log/nginx/*.log {
    daily
    missingok
    rotate 14
    compress
    # delaycompress # Uncomment this line to enable delaycompress (log files will be compressed / zipped starting from *.2 onwards)
    notifempty
    create 0640 www-data root
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
    endscript
}

3. Fix Permissions (Optional)

sudo chmod 640 /etc/nginx/log/*
sudo chown www-data:root /etc/nginx/log/*

4. Test Configuration

Dry run to check for errors

sudo logrotate -d /etc/logrotate.d/nginx

Force immediate rotation

sudo logrotate -vf /etc/logrotate.d/nginx

5. Verify Results

ls -lh /etc/nginx/log/

πŸ•˜ 6. Set Up Cron Job to Delete Old Logs

Create cleanup script (optional, but more maintainable)

sudo nano /usr/local/bin/nginx-log-cleanup.sh

Paste this into the file:

#!/bin/bash

# Number of days to keep logs
RETENTION_DAYS=30

# Log directories to clean
LOG_DIRS=(
    "/var/log/nginx"
    "/etc/nginx/log"
)

for DIR in "${LOG_DIRS[@]}"; do
    find "$DIR" -type f -name "*.gz" -mtime +$RETENTION_DAYS -exec rm -f {} \;
done

Make it executable:

sudo chmod +x /usr/local/bin/nginx-log-cleanup.sh

Schedule it via cron (runs daily at 2:30am):

sudo crontab -e

Add this line:

30 2 * * * /usr/local/bin/nginx-log-cleanup.sh >> /var/log/nginx-cleanup-cron.log 2>&1

Optional: Monitor cron logs

Check this log to ensure the cron job runs correctly:

tail -f /var/log/nginx-cleanup-cron.log

What Does 30 2 * * * Mean?

The cron expression 30 2 * * * breaks down as:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ Minute (0 - 59)
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ Hour (0 - 23)
β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ Day of Month (1 - 31)
β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ Month (1 - 12)
β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ Day of Week (0 - 7) (Sunday = 0 or 7)
β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚ β”‚
30 2 * * *

Meaning:

  • 30 β†’ at 30 minutes past the hour
  • 2 β†’ at 2 AM
  • * β†’ every day of the month
  • * β†’ every month
  • * β†’ every day of the week

βœ… This means the task runs at 2:30 AM every day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment