CentOS 下 Apache 连接数限制与优化
一 核心概念与生效位置
systemctl restart httpd。二 调整最大连接数的做法
httpd -l,输出中包含 mpm_prefork.c / mpm_worker.c / mpm_event.c 其一即为当前模块。Include conf/extra/httpd-mpm.conf),然后在对应 IfModule 段内调整参数。systemctl restart httpd。<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 1000
MaxRequestWorkers 500
MaxRequestsPerChild 10000
</IfModule>
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 50
ThreadLimit 64
MaxRequestWorkers 300
MaxConnectionsPerChild 0
</IfModule>
三 按 IP 或目录进行并发连接数限制
wget http://dominia.org/djao/limit/mod_limitipconn-0.24.tar.bz2
tar jxvf mod_limitipconn-0.24.tar.bz2
cd mod_limitipconn-0.24
make && make install # 或 /usr/local/apache2/bin/apxs -c -i mod_limitipconn.c
LoadModule limitipconn_module modules/mod_limitipconn.so
ExtendedStatus On
<Location />
MaxConnPerIP 6
NoIPLimit image/*
</Location>
<Location /mp3>
MaxConnPerIP 1
</Location>
四 相关网络与内核参数建议
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Timeout 60
五 监控与验证
httpd.conf 中开启状态页(示例):LoadModule status_module modules/mod_status.so
ExtendedStatus On
<Location /server-status>
SetHandler server-status
Require local
</Location>
访问 http://服务器IP/server-status?auto 查看当前连接与状态。ab -n 10000 -c 200 http://你的站点/
ps -ef | grep httpd | wc -l、netstat -an | grep :80 | wc -l、netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'top/htop、sar -n DEV 1 等,结合业务峰值与错误日志综合评估是否需要继续调优。