在 Debian 上“切割 Nginx 日志”通常指 log rotation(日志轮转),防止 access.log / error.log 无限增大。常见做法有 logrotate(推荐) 和 手动脚本/cron。
下面按最常用方式说明。
Debian 通常已为 Nginx 配置好 logrotate。
cat /etc/logrotate.d/nginx
典型内容类似:
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi
endscript
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
}
daily:每天切割rotate 14:保留 14 份compress:压缩旧日志notifempty:空日志不切割postrotate:切割后让 Nginx 重新打开日志(关键)logrotate -d /etc/logrotate.d/nginx
logrotate -f /etc/logrotate.d/nginx
logrotate 由 cron 每天运行:
/etc/cron.daily/logrotate
Debian 使用:
invoke-rc.d nginx rotate
或新版 systemd:
systemctl kill -s USR1 nginx
Nginx 收到 USR1 信号后会重新打开日志文件。
mv /var/log/nginx/access.log /var/log/nginx/access.log.$(date +%Y%m%d)
systemctl kill -s USR1 nginx
/var/lib/logrotate/status/var/log/nginx/*.logsize 100M
代替 daily
rotate 30
/var/log/nginx/*.log {
daily
size 50M
rotate 30
compress
delaycompress
missingok
notifempty
create 0640 www-data adm
sharedscripts
postrotate
systemctl kill -s USR1 nginx
endscript
}
如果你愿意,我可以:
直接说你的使用场景即可。