读懂 CentOS 上 Apache 日志,关键就两步:知道日志在哪 + 明白每行在说什么。下面用「实战角度」给你讲清楚。
CentOS 下 Apache 通常是 httpd:
# 常见位置
/var/log/httpd/access_log # 访问日志
/var/log/httpd/error_log # 错误日志
如果你用的是 源码或某些面板(如宝塔),可能在:
/usr/local/apache/logs/
确认 Apache 是否运行:
systemctl status httpd
192.168.1.10 - - [12/Apr/2024:10:22:31 +0800] "GET /index.html HTTP/1.1" 200 2326
| 字段 | 含义 |
|---|---|
192.168.1.10 |
客户端 IP |
- |
远程登录名(基本都是 -) |
- |
本地用户(未登录为 -) |
[12/Apr/2024:10:22:31 +0800] |
时间(东八区) |
"GET /index.html HTTP/1.1" |
请求方法、路径、协议 |
200 |
HTTP 状态码 |
2326 |
返回字节数 |
| 状态码 | 含义 | 说明 |
|---|---|---|
| 200 | 成功 | 正常 |
| 301 / 302 | 跳转 | 重定向 |
| 304 | 未修改 | 浏览器缓存 |
| 403 | 禁止 | 权限问题 |
| 404 | 找不到 | 路径错误 |
| 500 | 服务器错误 | PHP / 配置问题 |
| 502 | 网关错误 | 后端挂了 |
| 503 | 服务不可用 | 过载 / 维护 |
示例:
[Thu Apr 12 10:25:01.123456 2024] [core:error] [pid 1234] [client 1.2.3.4:5678]
AH00124: Could not open password file: /etc/httpd/.htpasswd
core, php, proxyerror, warn, noticetail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
awk '{print $1}' access_log | sort | uniq -c | sort -nr | head
grep " 404 " access_log
grep " 500 " access_log
可以,在 Apache 配置里:
vim /etc/httpd/conf/httpd.conf
找到:
LogFormat "..."
常见格式:
combined(最详细)common(基础)✅ 先会看:
✅ 再学:
如果你愿意,可以:
我可以直接帮你“逐行翻译”。