在 Debian 上,nginx 日志过大通常可以通过 日志轮转(logrotate)、关闭或降低日志级别、开启压缩 或 定期清理 来解决。下面按「推荐程度」给你一套实用方案。
Debian 默认会用 logrotate 管理 nginx 日志。
cat /etc/logrotate.d/nginx
典型内容:
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
示例(推荐):
/var/log/nginx/*.log {
size 100M
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
logrotate -d /etc/logrotate.d/nginx # 调试
logrotate -f /etc/logrotate.d/nginx # 强制执行
如果不需要详细日志:
access_log off;
或只记录关键信息:
log_format minimal '$remote_addr - $request_time';
access_log /var/log/nginx/access.log minimal;
error_log /var/log/nginx/error.log warn;
# 清空日志(不删文件,nginx 不中断)
truncate -s 0 /var/log/nginx/access.log
truncate -s 0 /var/log/nginx/error.log
或
echo "" > /var/log/nginx/access.log
df -h
du -sh /var/log/nginx/*
✅ 用 logrotate
✅ 开 compress
✅ rotate 7
✅ 不需要就 access_log off
如果你愿意,可以把 当前 nginx 配置 或 磁盘占用截图 发我,我可以直接帮你改好。