定位 Apache 中的慢请求日志,通常可以从以下几个层面入手:Apache 自带日志、模块扩展、系统/代理层分析。下面按实用程度由高到低说明。
Apache 默认访问日志不一定包含耗时,但可以通过 LogFormat 增加 %D 或 %T。
LogFormat "%h %l %u %t \"%r\" %>s %b %D" combined_time
CustomLog logs/access_log combined_time
字段说明:
%D:请求处理时间(微秒)%T:秒(整数)127.0.0.1 - - [10/Oct/2024:10:00:00] "GET /api/test HTTP/1.1" 200 1024 2500000
→ 2.5 秒(慢请求)
awk '$NF > 1000000' access_log
(>1 秒)
更清晰的方式:
LogFormat "%h %t \"%r\" %>s %b %Dms" slow_log
CustomLog logs/slow.log slow_log
然后:
grep -E "[0-9]{7}ms" logs/slow.log
<Location /server-status>
SetHandler server-status
Require local
</Location>
ExtendedStatus On
访问:
http://localhost/server-status
可看到:
示例:
LogSlowEnabled On
LogSlowLongRequestTime 1.0
LogSlowLogFile logs/slow_request.log
Apache 慢 ≠ Apache 本身慢,常见原因:
建议:
slowlog(php-fpm)top
iotop
strace -p <pid>
✅ 最快方案
给 Apache 访问日志加
%D,然后用脚本筛慢请求
✅ 长期方案
mod_log_slow + 应用层慢日志
如果你愿意,可以告诉我:
我可以给你一套可直接用的配置。