0
0
Nginxdevops~30 mins

Log rotation in Nginx - Mini Project: Build & Apply

Choose your learning style9 modes available
Log Rotation Setup for Nginx
📖 Scenario: You manage a web server using Nginx. The server creates log files that grow over time. To keep your server clean and avoid running out of disk space, you need to set up log rotation. Log rotation means saving old logs and starting fresh ones regularly.
🎯 Goal: Set up a basic log rotation configuration for Nginx logs using a configuration file. You will create the initial log rotation config, add rotation settings, apply the rotation command, and finally check the rotated logs.
📋 What You'll Learn
Create a log rotation configuration file for Nginx logs
Set the rotation frequency to daily
Keep 7 rotated log files
Compress old log files
Reload Nginx logs after rotation
💡 Why This Matters
🌍 Real World
Web servers generate large log files that can fill up disk space. Log rotation helps keep logs manageable and the server running smoothly.
💼 Career
System administrators and DevOps engineers regularly configure log rotation to maintain server health and troubleshoot issues efficiently.
Progress0 / 4 steps
1
Create the initial log rotation config file
Create a file called /etc/logrotate.d/nginx with the content starting with /var/log/nginx/*.log { on the first line and ending with } on the last line.
Nginx
Need a hint?

Use a text editor or echo command to create the file with the required lines.

2
Add rotation frequency and retention settings
Inside the /etc/logrotate.d/nginx file, add the lines daily, rotate 7, and compress between the braces to set daily rotation, keep 7 files, and compress old logs.
Nginx
Need a hint?

Each setting goes on its own line inside the braces.

3
Add command to reload Nginx after rotation
Add the line postrotate followed by systemctl reload nginx and endscript inside the braces to reload Nginx after logs rotate.
Nginx
Need a hint?

Indent the commands inside postrotate and endscript for clarity.

4
Test the log rotation and check rotated logs
Run the command logrotate -f /etc/logrotate.d/nginx to force log rotation. Then run ls /var/log/nginx/ to list the log files and verify rotated logs with extensions like .1.gz exist.
Nginx
Need a hint?

Use logrotate -f to force rotation and ls to check files.