使用mod_bandwidth可以实现并发Ip数和带宽的限制,带宽流量控制模块
安装
tar zxvf mod_bw-0.7.tgz
cd mod_bw
apxs -c -i mod_bw.c
配置
1 全局开启
vim /etc/httpd/conf.modules.d/mod_bw.conf
2 目录限速
LoadModule bw_module modules/mod_bw.so
<virtualHost *:80>
ServerName vhost1.cszhi.com
DocumentRoot /var/www/vhost1
BandwidthModule On
ForceBandWidthModule On
Bandwidth all 1024000
MinBandwidth all 50000 #每个客户端最高速度可达50KB
LargeFileLimit * 500 50000
MaxConnection all 6
</virtualHost>
安装中可能遇到的问题
1 提示如下错误:
/mnt/vdd/mod_bw.c: In function 'get_bw_rate':
/mnt/vdd/mod_bw.c:567:59: error: 'conn_rec' has no member named 'remote_addr'
if (apr_ipsubnet_test(e[i].x.ip, r->connection->remote_addr)) {
这个错误是由于apache2.2 到2.4 api有所改变,详细如下:
http://httpd.apache.org/docs/2.4/developer/new_api_2_4.html
vim mod_bw.c
把所有的remote_ip和remote_addr分别替换成client_ip和client_addr
:%s/remote_ip/client_ip/
:%s/remote_addr/client_addr/
重新执行 apxs -i -c -a mod_bw.c
2 httpd服务重启后发生错误
[root@vps httpd]# /etc/init.d/httpd restart
Stopping httpd: [FAILED]
Starting httpd: httpd: Syntax error on line 203 of /etc/httpd/conf/httpd.conf: Cannot load /etc/httpd/modules/mod_bw.so into server: /etc/httpd/modules/mod_bw.so: undefined symbol: apr_atomic_cas
[FAILED]
修改mod_bw.c
#ifdef APR_MAJOR_VERSION //添加这行
#if (APR_MAJOR_VERSION < 1)
#define apr_atomic_inc32 apr_atomic_inc
#define apr_atomic_dec32 apr_atomic_dec
#define apr_atomic_add32 apr_atomic_add
#define apr_atomic_cas32 apr_atomic_cas
#define apr_atomic_set32 apr_atomic_set
#endif
#endif //添加这行
3 apxs没有安装
安装httpd-devel
配置参考
http://www.361way.com/apache-limit-speed/2138.html