在 CentOS 上定位 Apache 慢请求(慢日志),可以按下面几个思路来做。
Apache 默认 没有像 Nginx 那样专门的 slow log,但可以通过以下方式定位慢请求:
路径通常为:
/var/log/httpd/access_log
查看响应时间字段(如果有):
tail -f /var/log/httpd/access_log
如果日志格式包含 %D 或 %T:
%D:请求耗时(微秒)%T:请求耗时(秒)例如:
127.0.0.1 - - [time] "GET /api HTTP/1.1" 200 1234 1050000
表示 1.05 秒
grep -R "LogFormat" /etc/httpd/
常见默认格式:
LogFormat "%h %l %u %t \"%r\" %>s %b" common
✅ 建议改成:
LogFormat "%h %l %u %t \"%r\" %>s %b %D" combined
然后重启 Apache:
systemctl restart httpd
mod_log_config + 慢请求过滤awk '{print $NF}' /var/log/httpd/access_log | sort -n | tail
或带时间字段:
awk '{if ($NF > 1000000) print}' /var/log/httpd/access_log
如果你是 PHP + Apache(mod_php / php-fpm):
编辑:
/etc/php-fpm.d/www.conf
启用:
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 2s
重启:
systemctl restart php-fpm
查看:
tail -f /var/log/php-fpm/slow.log
✅ 这是定位 PHP 慢的最有效方式
httpd -M | grep status
启用:
<Location /server-status>
SetHandler server-status
Require local
</Location>
访问:
http://localhost/server-status
如果是“整体慢”:
top
htop
iostat -x 1
netstat -an | grep :80 | wc -l
最佳实践:
%Dslowlogtail + awk 快速筛慢请求如果你能告诉我:
我可以给你更精确的配置示例。