This guide configures logrotate to keep Nginx logs for ~75 days, rotating daily, compressing old logs, and safely signaling Nginx to reopen files after rotation.
Please answer these so I can tailor the exact config:
/var/log/nginx/access.log
(or per-site: /var/log/nginx/gbc_access.log
)/var/log/nginx/error.log
(or per-site: /var/log/nginx/gbc_error.log
)www-data
)adm
)/run/nginx.pid
)Once confirmed, substitute below or I’ll produce a finalized snippet for you.
Create or overwrite /etc/logrotate.d/nginx
with a 75‑day policy.
sudo tee /etc/logrotate.d/nginx >/dev/null <<'EOF'
/var/log/nginx/*access*.log /var/log/nginx/*error*.log {
daily
rotate 75 # keep ~75 days of archives
dateext # append date to rotated files
missingok
notifempty
compress
delaycompress
sharedscripts
create 0640 www-data adm
postrotate
# Signal nginx to reopen logs after rotation
[ -s /run/nginx.pid ] && kill -USR1 $(cat /run/nginx.pid) 2>/dev/null || true
endscript
}
EOF
Notes:
www-data adm
if your user/group differ./var/log/nginx/gbc_access.log
), either:
Check your active config to confirm log paths:
sudo nginx -T | grep -nE 'access_log|error_log'
If paths are outside /var/log/nginx/
, add those exact files to the first line in /etc/logrotate.d/nginx
.
sudo logrotate -d /etc/logrotate.conf
You should see it reference /etc/logrotate.d/nginx
and list the targeted log files.
sudo logrotate -f /etc/logrotate.conf
Then list rotated files:
ls -lh /var/log/nginx/
Expect files like access.log-YYYYMMDD.gz
to appear.
Make a request to your site, then ensure access.log
is still growing:
sudo tail -n 5 /var/log/nginx/access.log
If needed, verify Nginx still has file descriptors open to the new files:
sudo ls -l /proc/$(cat /run/nginx.pid)/fd | grep '/var/log/nginx' || true
On Ubuntu, logrotate runs via systemd.
systemctl status logrotate.timer 2>/dev/null || true
If inactive, enable it:
sudo systemctl enable --now logrotate.timer
rotate
separately for access vs error logs./etc/systemd/journald.conf
with MaxRetentionSec=75day
(not typical for Nginx file logs).Reply with:
I’ll produce a finalized /etc/logrotate.d/nginx
tailored to your droplet.