温馨提示×

centos apache日志如何过滤

小樊
44
2026-09-30 07:01:20
栏目: 智能运维

在 CentOS 上,Apache 的日志通常位于:

  • 访问日志:/var/log/httpd/access_log
  • 错误日志:/var/log/httpd/error_log

下面按常见需求给你一些日志过滤方法(命令行为主)。


一、按关键字过滤(最常用)

1. 查看包含某个 IP 的访问记录

grep "192.168.1.100" /var/log/httpd/access_log

2. 查看某个 URL 的访问

grep "/login.php" /var/log/httpd/access_log

3. 查看 404 错误

grep " 404 " /var/log/httpd/access_log

4. 查看错误日志中的 PHP 错误

grep -i "php" /var/log/httpd/error_log

二、按时间过滤

1. 查看今天的日志

grep "$(date +%d/%b/%Y)" /var/log/httpd/access_log

2. 查看某个时间段(如 10:00–11:00)

awk '$4 >= "[10/Apr/2024:10:00:00" && $4 <= "[10/Apr/2024:11:00:00"' /var/log/httpd/access_log

三、统计类过滤

1. 统计访问最多的 IP

awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -nr | head

2. 统计访问最多的 URL

awk '{print $7}' /var/log/httpd/access_log | sort | uniq -c | sort -nr | head

3. 统计状态码分布

awk '{print $9}' /var/log/httpd/access_log | sort | uniq -c | sort -nr

四、实时查看并过滤

1. 实时查看访问日志

tail -f /var/log/httpd/access_log

2. 实时过滤某个 IP

tail -f /var/log/httpd/access_log | grep "192.168.1.100"

五、使用高级工具(推荐)

1. 使用 less(可搜索)

less /var/log/httpd/access_log
  • 输入 /关键字 搜索
  • G 跳到末尾

2. 使用 journalctl(systemd 环境)

Apache 错误也可能在系统日志:

journalctl -u httpd

3. 使用 goaccess(可视化分析)

yum install goaccess
goaccess /var/log/httpd/access_log

六、日志格式说明(方便过滤)

默认 combined 格式:

IP - - [时间] "请求" 状态码 大小 "Referer" "User-Agent"

如果你有具体过滤场景(比如:只看成拜占庭请求、只看某个域名、只看 POST 请求),可以告诉我,我给你更精确的命令。

0 踩