Skip to content

Instantly share code, notes, and snippets.

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

  • Save kanchokanchev/5bb1cc95778c56f6c4f72ab2c52dc029 to your computer and use it in GitHub Desktop.

Select an option

Save kanchokanchev/5bb1cc95778c56f6c4f72ab2c52dc029 to your computer and use it in GitHub Desktop.
Nginx - Log Management Guide #Nginx_ADMIN #Nginx_Log

Nginx Log Management Guide

Efficient log management helps in maintaining disk space and improving observability. This guide covers how to find, inspect, and manage old Nginx logs.


πŸ“ 1. Find and Manage Old Log Files

πŸ” List Compressed Logs Older Than N Days

find /etc/nginx/log/ -name "*.gz" -mtime +N

Replace N with the number of days.

Examples:

# Logs older than 30 days
find /etc/nginx/log/ -name "*.gz" -mtime +30

# Logs older than 7 days
find /etc/nginx/log/ -name "*.gz" -mtime +7

πŸ“„ List With File Details (Size, Modification Date)

find /etc/nginx/log/ -name "*.gz" -mtime +30 -ls

πŸ”’ Count Old Log Files

find /etc/nginx/log/ -name "*.gz" -mtime +30 | wc -l

⚠️ Delete Old Logs (Be Careful!)

Dry Run (Preview What Will Be Deleted)

find /etc/nginx/log/ -name "*.gz" -mtime +30 -print

Actual Deletion

find /etc/nginx/log/ -name "*.gz" -mtime +30 -delete

πŸ—‘οΈ Find and Delete Empty Log Files

Find Empty Log Files

find /etc/nginx/log/ -type f -empty -name "*.log"

Delete Empty Log Files

find /etc/nginx/log/ -type f -empty -name "*.log" -delete

βœ… Best Practices

  • Always test with -print before using -delete
  • Consider compressing instead of deleting:
find /etc/nginx/log/ -name "*.log" -mtime +7 -exec gzip {} \;
  • Set up monitoring for log directory size:
du -sh /etc/nginx/log/

πŸ” Log Rotation Verification

Check when logs were last rotated:

ls -lth /etc/nginx/log/*.gz | head -n 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment