Apache性能优化需聚焦“快不快、错不错、多不多”三维度,重点排查返回200但耗时超2秒的路径;须在LogFormat中启用%D字段记录微秒级响应时间,并用awk命令分析日志定位慢接口。
直接看日志里“快不快、错不错、多不多”三个维度,就能揪出Apache真正的性能死角。不是只盯500错误,而是找那些返回200却耗时2秒以上的路径——它们才是拖垮用户体验的隐形瓶颈。
Apache默认日志不记录处理耗时,必须在LogFormat中显式加入%D(微秒级)或%T(秒级)。推荐用%D,精度高、干扰小:
grep LogFormat /etc/httpd/conf/httpd.conf 或 /etc/apache2/apache2.conf
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %D %U" combined_with_time
sudo systemctl restart apache2(Debian)或 sudo systemctl restart httpd(CentOS)不用装任何工具,一条awk就能定位问题接口:
awk '$9 ~ /^[2-5]..$/ && $10 > 1200000 {print $7, $10}' /var/log/apache2/access.log | awk '{sum[$1]+=$2; cnt[$1]++} END {for (p in sum) print int(sum[p]/cnt[p]), p}' | sort -nr | head -5
awk '/[14:..:.. / {print $4,$7,$10}' /var/log/apache2/access.log | awk '{t=substr($1,2,5); if(t>="14:00" && t
单看慢,容易误判。要叠加三个信号:
error_log里没有“error”字眼,也可能暴露性能死角:
server reached MaxRequestWorkers → 进程数不足,需调MaxRequestWorkers或换MPM模式client denied by server configuration → 可能因Require规则嵌套过深,每次请求都做多次ACL校验mod_proxy: error reading status line from remote server → 后端响应慢或断连,Apache在等,拖长整体耗时