日志路径与格式
/etc/httpd/conf/httpd.conf,通过 ErrorLog 和 CustomLog 指令设置路径与格式,例如:ErrorLog "/var/log/httpd/error_log"
CustomLog "/var/log/httpd/access_log" combined # 常用格式,记录IP、请求方法、状态码等
LogFormat 指令,如仅记录IP和请求路径:LogFormat "%h %r" minimal
CustomLog "/var/log/httpd/minimal_access.log" minimal
日志级别调整
LogLevel 指令控制日志详细程度,可选 debug、info、warn、error 等,降低级别可减少日志量,例如:LogLevel warn # 仅记录警告及以上级别日志
/etc/logrotate.d/httpd,设置轮转规则:/var/log/httpd/*.log {
daily # 每天轮转
rotate 7 # 保留7天日志
compress # 压缩旧日志
missingok # 忽略丢失文件
notifempty # 非空文件才轮转
postrotate
systemctl reload httpd # 轮转后重启Apache
endscript
}
sudo logrotate -d /etc/logrotate.d/httpd # 调试模式
sudo logrotate -f /etc/logrotate.d/httpd # 强制执行
实时监控与分析
tail -f /var/log/httpd/access_log # 实时查看访问日志
grep "关键字" /var/log/httpd/error_log # 过滤错误日志
GoAccess:生成可视化报告,支持实时分析,安装后运行:sudo yum install goaccess
goaccess /var/log/httpd/access_log -o report.html --log-format=COMBINED
ELK Stack:用于大规模日志的集中管理、搜索和可视化。性能优化
mpm_event 模块并设置 MaxConnectionsPerChild 等参数,减少日志写入阻塞。安全与权限管理
sudo chown -R apache:apache /var/log/httpd
sudo chmod -R 750 /var/log/httpd
SetEnvIf 指令排除特定请求的日志记录,例如过滤掉健康检查请求。