在 CentOS 上“定位 Apache 慢日志”通常分三步:确认是否开启慢日志 → 找到慢日志位置 → 分析慢请求。下面按实战流程说明。
Apache 本身没有像 MySQL 那样的 slow log,但可以通过以下两种方式实现“慢请求日志”:
最常用的是 访问日志中记录响应时间。
httpd -v
rpm -qc httpd
常见配置文件:
/etc/httpd/conf/httpd.conf/etc/httpd/conf.d/*.conf%D 或 %Tgrep -R "LogFormat" /etc/httpd/
示例(推荐):
LogFormat "%h %l %u %t \"%r\" %>s %b %D" combined_time
说明:
%D:请求处理时间(微秒)%T:秒(不够精确)编辑配置文件:
vi /etc/httpd/conf/httpd.conf
修改或新增:
LogFormat "%h %l %u %t \"%r\" %>s %b %D" slowlog
CustomLog logs/slow_log combined_time
或单独:
CustomLog logs/access_time.log combined_time
重启 Apache:
systemctl restart httpd
ls -l /var/log/httpd/
常见:
access_logerror_logslow_log(如果你新建的)假设 %D 是微秒:
awk '{if ($NF > 1000000) print}' /var/log/httpd/access_log
或只看 URL:
awk '($NF+0 > 1000000){print $7}' /var/log/httpd/access_log
awk '{print $NF, $7}' /var/log/httpd/access_log | sort -nr | head
tail -f /var/log/httpd/error_log
重点关注:
timed outchild processsegfaultAH0xxxhttpd -M | grep status
<Location /server-status>
SetHandler server-status
Require ip 127.0.0.1
</Location>
访问:
http://localhost/server-status
apachetopyum install epel-release -y
yum install apachetop -y
apachetop -f /var/log/httpd/access_log
LogFormat 有 %Daccess_log 筛 > 1s 请求error_log 看报错server-status如果你愿意,可以把 Apache 版本 + 一段匿名化日志 发出来,我可以帮你直接分析慢在哪。