在 Ubuntu 上,Apache 日志(通常是 /var/log/apache2/access.log 和 error.log)突然或逐渐变大,常见原因有以下几种:
✅ 表现:
access.log 快速增长✅ 排查:
tail -n 100 /var/log/apache2/access.log
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head
ErrorLog 级别太低,会记录大量信息✅ 检查:
grep -i loglevel /etc/apache2/apache2.conf
✅ 建议:
LogLevel warn
Ubuntu 默认有 logrotate,但如果:
日志会一直追加,不切割。
✅ 检查:
cat /etc/logrotate.d/apache2
ls -lh /var/log/apache2/
✅ 手动测试:
logrotate -d /etc/logrotate.d/apache2
.htaccess 重定向死循环✅ 表现:
error.log 暴涨✅ 排查:
tail -n 50 /var/log/apache2/error.log
自定义 LogFormat 包含:
%{Referer}i%{User-Agent}i✅ 检查:
grep LogFormat /etc/apache2/apache2.conf
✅ 可忽略或单独记录:
SetEnvIf Request_URI "^/health" dontlog
CustomLog ${APACHE_LOG_DIR}/access.log combined env=!dontlog
lsof | grep deleted
如果 Apache 还持有旧文件,磁盘不会释放。
✅ 解决:
systemctl restart apache2
# 确保 logrotate 正常工作
systemctl status logrotate
# 限制日志大小
# 在 logrotate 中设置:
size 100M
rotate 7
如果你愿意,可以贴出:
access.log 示例df -hls -lh /var/log/apache2我可以帮你精确判断是哪种原因。