apache日志分析采用elk栈实现分布式采集与分析:filebeat轻量采集access.log/error.log,logstash用combinedapachelog模式解析,elasticsearch按日期索引存储,kibana构建ip、状态码、响应时间等可视化仪表板。

Apache 日志分析用 ELK 栈搭建分布式日志平台,核心是把分散在各台 Web 服务器上的 access.log 和 error.log 统一采集、结构化解析、集中存储,并支持按 IP、状态码、URL 路径、响应时间等字段快速检索与可视化。整个过程不依赖 Apache 自带模块,而是通过轻量采集器 + Logstash 清洗 + Elasticsearch 存储 + Kibana 展示来实现。
Apache 日志接入前的关键准备
确保每台运行 Apache 的服务器已开启标准日志格式,并保留必要字段:
-
推荐使用 Combined 日志格式(默认或显式配置):
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
该格式包含客户端 IP、请求时间、HTTP 方法、URI、状态码、字节数、来源页和 UA,是后续结构化解析的基础。 -
避免日志轮转干扰采集:建议将
rotatelogs或logrotate配置为每日轮转、不压缩(如daily+nodateext),或让 Filebeat 支持压缩文件读取(需启用gzip插件)。 -
确认日志路径统一可访问:例如全部存于
/var/log/apache2/access.log和/var/log/apache2/error.log,便于批量配置采集器。
用 Filebeat 替代 Logstash 直连采集(推荐生产方案)
Logstash 资源开销大,不适合部署在每台 Apache 服务器上。应采用轻量级 Filebeat 作为前端采集器,将日志转发至中心 Logstash 或直接写入 Elasticsearch。
Apache Superset 是一个广泛采用的开源 BI 平台,用于 SQL 探索、图表构建和仪表板交付。当代理需要查询仓库数据、组装仪表板或使用成熟的分析界面解释指标而不是临时笔记本代码时,此技能非常有用。
- 在每台 Apache 服务器安装 Filebeat:
sudo apt install filebeat(Ubuntu/Debian)或sudo yum install filebeat(RHEL/CentOS) - 编辑
/etc/filebeat/filebeat.yml,添加两个输入模块:
- type: filestream
enabled: true
paths:
- /var/log/apache2/access.log
fields:
log_type: apache_access
server_name: web01 # 按实际主机名填写
- type: filestream
enabled: true
paths:
- /var/log/apache2/error.log
fields:
log_type: apache_error
server_name: web01 - 输出指向中心 Logstash(推荐):
output.logstash:
hosts: ["logstash-server:5044"]
ssl.enabled: false
或直连 Elasticsearch(简化架构):
output.elasticsearch:
hosts: ["http://es-cluster:9200"]
username: "elastic"
password: "your_strong_password" - 启动并设为开机自启:
sudo systemctl enable --now filebeat
Logstash 管道中精准解析 Apache 日志
在中心 Logstash 服务中,配置一个专用管道(如 /etc/logstash/conf.d/apache.conf),重点完成三件事:识别日志类型、Grok 解析、时间字段标准化。
- 输入段匹配 Filebeat 发送的
log_type字段:
input {
beats { port => 5044 }
} - 过滤段使用内置 Grok 模式(无需手写正则):
filter {
if [fields][log_type] == "apache_access" {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
date {
match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]
target => "@timestamp"
}
# 可选:提取 URL 路径中的关键参数
dissect { mapping => { "request" => "%{method} %{path}?%{querystring} %{protocol}" } }
}
if [fields][log_type] == "apache_error" {
grok { match => { "message" => "%{APACHE_ERROR_LOG}" } }
}
} - 输出段按日期动态建索引:
output {
elasticsearch {
hosts => ["http://es-cluster:9200"]
user => "elastic"
password => "your_strong_password"
index => "apache-%{[fields][log_type]}-%{+YYYY.MM.dd}"
}
}
Kibana 中构建 Apache 专属分析视图
数据进入 Elasticsearch 后,在 Kibana 中完成最后一步:定义索引模式、创建仪表板、设置常用搜索与告警。
-
创建索引模式:访问
http://kibana-host:5601→ Stack Management → Index Patterns → 创建新模式,填入apache-*,选择@timestamp作为时间字段。 -
验证字段解析效果:进入 Discover 页面,筛选
apache_access-*索引,检查是否出现clientip、response、request、bytes、urlpath等结构化字段。 -
高频分析场景示例:
– 查看 Top 10 访问 IP:Filter: log_type: "apache_access"→ Visualize → Horizontal Bar Chart,X-axis 选clientip.keyword,Aggregation 选 Count;
– 分析 5xx 错误趋势:Filter: response >= 500→ Lens 图表,X-axis 时间,Y-axis Count,Split Series 选response;
– 定位慢请求(>2s):Filter: response_time > 2000→ 查看对应request和referrer字段。 - 保存为重用仪表板:将多个可视化组件加入 Dashboard,命名为 “Apache Traffic & Errors”,供运维团队日常巡检。










