在 CentOS 上过滤 Apache 日志,通常是对 访问日志(access log) 或 错误日志(error log) 进行筛选。下面按常见场景给你一套实用方法。
# 访问日志
/var/log/httpd/access_log
# 错误日志
/var/log/httpd/error_log
(如果你用 mod_ssl,还有 ssl_access_log 等)
grep "192.168.1.100" /var/log/httpd/access_log
grep " 404 " /var/log/httpd/access_log
grep "/login.php" /var/log/httpd/access_log
grep "10/Apr/2024" /var/log/httpd/access_log
awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -nr
grep " 404 " /var/log/httpd/access_log | awk '{print $7}' | sort | uniq -c | sort -nr
grep "192.168.1.100" /var/log/httpd/access_log | awk '{print $4}' | cut -d: -f2 | sort | uniq -c
CentOS 默认已启用 logrotate:
/etc/logrotate.d/httpd
查看旧日志:
ls /var/log/httpd/
zcat /var/log/httpd/access_log-20240410.gz | grep "404"
tail -f /var/log/httpd/access_log | grep "503"
如果你用了 CustomLog 分文件:
/var/log/httpd/www.example.com-access_log
直接过滤对应文件即可。
yum install goaccess
goaccess /var/log/httpd/access_log -o report.html
如果你有 具体过滤需求(如:统计爬虫、封 IP、分析慢请求),可以告诉我,我可以直接给你命令。