Rotation of Nginx logs
Over time, Nginx access and error logs can grow significantly in size, consuming valuable disk space. To keep logs manageable and maintain server health, it's best practice to rotate and clean up old logs on a schedule.
Note: This cron job runs on the host machine, not inside a Docker container.
1. Ensure the Rotation Script is Executable
On the host machine, make the log rotation script executable:
chmod +x /root/wuwei-docker/nginx/logrotate.sh
2. Schedule Daily Log Rotation with Cron
Open the crontab editor:
crontab -e
Add the following line to rotate logs daily at 1:00 AM:
0 1 * * * /usr/bin/docker exec wuwei-nginx bash /etc/nginx/logrotate.sh
3. Restart Nginx Container
Restart the container to apply changes:
docker restart wuwei-nginx
4. Test the Rotation Script
Run the following command manually:
docker exec wuwei-nginx bash /etc/nginx/logrotate.sh
check the log directory on the host:
ls /root/wuwei-docker/nginx/log
You should see files like:
wuwei.access.20250628.log.gz
wuwei.error.20250628.log.gz