Nginx 日志过大通常可以从日志级别、日志内容、切割归档、异步写入、磁盘与采集方式几个方向来优化。下面按「立竿见影 → 长期方案」给你一套实用做法。
error_log /var/log/nginx/error.log warn;
常用级别(从少到多):
crit < error < warn < notice < info < debug生产环境一般用
warn或error
如果某些接口不需要日志:
location /health {
access_log off;
}
或完全关闭:
access_log off;
默认 combined 日志很大,可自定义:
log_format minimal '$remote_addr - $request_time $status';
access_log /var/log/nginx/access.log minimal;
location ~* \.(js|css|png|jpg|gif|ico)$ {
access_log off;
}
/etc/logrotate.d/nginx 示例:
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
sharedscripts
postrotate
nginx -s reload
endscript
}
效果:
access_log /var/log/nginx/access.log main buffer=64k flush=1m;
优点:
logrotate 中已加 compress 即可
access_log syslog:server=127.0.0.1:514 main;
常见问题:
可快速看:
tail -f /var/log/nginx/access.log
awk '{print $9}' access.log | sort | uniq -c | sort -nr
✅ warn 错误日志
✅ buffer + flush
✅ logrotate 每日压缩
✅ 静态资源 access_log off
✅ 非核心接口不记录
如果你愿意,可以贴一段 nginx.conf 或日志样本,我可以直接帮你改成“最小日志方案”。