在 Debian 上过滤 Apache 日志,常见需求有 实时过滤、按条件提取、统计、排除 等。下面按使用场景给你一套实用方法(适用于 Debian 10/11/12,Apache 2.4)。
# 访问日志
/var/log/apache2/access.log
/var/log/apache2/other_vhosts_access.log
# 错误日志
/var/log/apache2/error.log
grep "1.2.3.4" /var/log/apache2/access.log
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -nr | head
grep "10/Jan/2026" /var/log/apache2/access.log
grep "10/Jan/2026:14:" /var/log/apache2/access.log
awk '$9 == 404' /var/log/apache2/access.log
或:
grep '" 404 ' /var/log/apache2/access.log
grep '" 500 ' /var/log/apache2/access.log
grep "/api/login" /var/log/apache2/access.log
grep '"POST ' /var/log/apache2/access.log
grep -v "127.0.0.1" /var/log/apache2/access.log
grep -v -E "\.(css|js|png|jpg|gif)" /var/log/apache2/access.log
tail -f /var/log/apache2/access.log | grep "404"
只监控 500 错误:
tail -f /var/log/apache2/error.log
goaccess(强烈推荐)sudo apt install goaccess
goaccess /var/log/apache2/access.log --log-format=COMBINED
或生成 HTML 报告:
goaccess /var/log/apache2/access.log \
--log-format=COMBINED \
-o /var/www/html/report.html
浏览器打开:
http://服务器IP/report.html
Debian 默认使用:
/var/log/apache2/other_vhosts_access.log
格式:
example.com:80 1.2.3.4 - - [time] "GET / HTTP/1.1" 200 ...
grep "example.com" /var/log/apache2/other_vhosts_access.log
zcat /var/log/apache2/access.log.1.gz | grep "404"
或:
zgrep "404" /var/log/apache2/access.log.1.gz
awk 做复杂过滤(示例)只看访问量 > 100 的 IP:
awk '{ip[$1]++} END {for (i in ip) if (ip[i]>100) print ip[i], i}' \
/var/log/apache2/access.log
✅ 实时看 404
✅ 统计访问 IP
✅ 排除静态资源
✅ 按时间 / 状态码 / URL 过滤
✅ 用 goaccess 可视化
如果你愿意,可以告诉我:
我可以给你 精准的一行命令或脚本。