在 Debian 上 Nginx 日志太大,常见处理方式有 日志切割、压缩归档、限制大小、关闭不必要日志、集中清理 等。下面按「实用程度」给你一套做法。
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
}
daily → 每天切(也可 size 100M)rotate 7~14 → 保留天数compress → 压缩旧日志size 100M → 超过才切(适合访问量暴增)示例(按大小切):
/var/log/nginx/*.log {
size 100M
rotate 10
compress
missingok
notifempty
create 0640 www-data adm
sharedscripts
postrotate
kill -USR1 `cat /var/run/nginx.pid`
endscript
}
logrotate -d /etc/logrotate.d/nginx # 模拟
logrotate -f /etc/logrotate.d/nginx # 强制执行
# 只看大小
du -sh /var/log/nginx/*
# 清空(不删文件,Nginx 不中断)
truncate -s 0 /var/log/nginx/access.log
truncate -s 0 /var/log/nginx/error.log
⚠️ 不要直接 rm,否则 Nginx 还占用 inode。
server {
access_log off;
}
或只记录错误:
access_log /var/log/nginx/access.log compression;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?)$ {
access_log off;
}
error_log /var/log/nginx/error.log warn;
rsyslog / filebeat → ESaccess_log syslog:server=...;du -sh /var/log/nginx/*
ls -lh /var/log/nginx/
logrotate -d /etc/logrotate.d/nginx
如果你愿意,可以告诉我:
access.log 还是 error.log 大?我可以直接帮你写一份 最合适的 logrotate + nginx 配置。