在 Ubuntu 上,Apache 日志(通常是 access.log 和 error.log)变得过大的原因一般有以下几种常见情况:
Ubuntu 默认使用 logrotate 管理 Apache 日志,但如果:
logrotate 被禁用或损坏LogLevel 设置太低(如 debug、info)rewrite, ssl, proxy).env、.git 探测)LogFormat 包含大量字段%{Referer}i %{User-Agent}i 等冗余信息例如:
cat /etc/logrotate.d/apache2
手动执行测试:
sudo logrotate -d /etc/logrotate.d/apache2
编辑 Apache 配置:
LogLevel warn
忽略静态文件:
SetEnvIf Request_URI "\.(jpg|css|js|png)$" static
CustomLog ${APACHE_LOG_DIR}/access.log combined env=!static
LogLevel error
<Directory />
Require all granted
Require not ip 1.2.3.4
</Directory>
sudo du -sh /var/log/apache2/*
如果你愿意,可以把:
apache2.conf 或 sites-enabled 配置logrotate 内容发出来,我可以帮你精确定位是哪一种原因。