日期:2014-05-17  浏览次数:20929 次

Apache带宽流量控制模块安装 mod_bw
I. 下载mod_bw
从http://bwmod.sourceforge.net/下载mod_bw

II. 安装mod_bw
tar zxvf mod_bw-0.7.tgz
cd mod_bw
修改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  //新增行

运行
# /usr/local/apache/bin/apxs -c -i -a mod_bw.c

III. 配置mod_bw
打开httpd.conf.
BandWidthModule On
ForceBandWidthModule On #这个没加上之前试了几次发现不起作用
BandWidth     all 102400
MinBandWidth all 51200
MaxConnection all 50


mod_bw 是一个apche的第三方插件,可以实现即时带宽控制,连接数控制,对于需要对站点限速的朋友来讲是个不错的选择,下面将几个具体的配置项做以说明:

先写一个典型的配置例子:
BandWidthModule On
ForceBandWidthModule On  
BandWidth all 102400  #限制为100K
MaxConnection all 100 #限制为100个连接数

可写在全局配置和单个虚拟主机中。

IV. Configuration Directives
1 - BandWidthModule [On|Off]
这个 module 预设是关闭的,要将他开启才能够使用。

Example:
BandWidthModule On 2 - ForceBandWidthModule [On|Off]
这个 module 预设不会过滤每个需求。
如果您开启他,他将处理过滤每个需求。

Example :
(正常的使用下,仅会过滤 text/html test/plain)
AddOutputFilterByType MOD_BW text/html text/plain

(开启的状况下)
2 - ForceBandWidthModule On

3 - BandWidth [From] [bytes/s]
这边有两个参数。
From 是限制来源的位置,也就是该位置受限制。他可以是完整的 hostname、网域名称或 IP。可搭配遮罩使用,例如 192.168.0.0/24 or 192.168.0.0/255.255.255.0 。
另一个参数是限制的速率,以 bytes 每秒为单位;假如为 0,则不受限制。
Example :
BandWidth localhost 10240
BandWidth 192.168.218.5 0
( 依照设定的先后顺序为排序准则。Order is relevant. First entries have precedence )

4 - MinBandWidth [From] [bytes/s]
这边有两个参数。
From 是限制来源的位置,也就是该位置受限制。他可以是完整的 hostname、网域名称或 IP。可搭配遮罩使用,例如 192.168.0.0/24 or 192.168.0.0/255.255.255.0 。
另一个参数每个连线限制的最小速率,以 bytes/s 为单位,-1 代表无限制。

Examples :
BandWidth all 102400
MinBandWidth all 50000

The example above, will have a top speed of 100kb for the 1? client. If more clients come, it will be splitted accordingly but
everyone will have at least 50kb (even if you have 50 clients)

BandWidth all 50000
MinBandWidth all -1

上面的例子说明每个连线有 50kb 的速度(不限制最小速度)

5 - LargeFileLimit [Type] [Minimum Size] [bytes/s]
顾名思义,这设定是专门用来限制大型档案的。
Type 是指副档名,可以使用 * 代表全部。也可使用 .tgz 、 .avi 等。
Minimun Size 单位是 kbytes/s,只要超过这个 Size 就被规范在这个设定的限速中。
最后一个参数就是被限制的速率囉!

Example :
LargeFileLimit .avi 500 10240

This limits .avi files over (or equal to) 500kb to 10kbytes/s

6 - BandWidthPacket [Size]
可能您不需要去设定这个参数!
预设值为 8192,适用于任何速度。
这个设定必须介于 1024 至 131072。
小的封包将使得速度变慢,且更耗费系统效能;相反亦是。

7 - BandWidthError [Error]
这个选项是用来自订个人化错误讯息的。
在预设的情况下,超过最大连线时,这个 module 将会丢出 503 HTTP_SERVICE_UNAVAILABLE 回应。
对于大部分的人来说,他们会困扰着错误讯息,不知道为什么会这样。
你可以自订一个错误讯息的页面,去解释在什么情况下会发生这种问题。
但有时候错误号码 503 是不适用这个地方的。
所以你可以自订一个错误号码从 300 至 599。
( 有关 HTTP 错误讯息可参考下列 Reference: HTTP Protocol Error Codes)
在自订错误编码时请注意,要使用尚未被定义的号码!

测试的时候,我们使用错误号码 510(510尚未被定义)

And Example, with Personalized Error Page :

ErrorDocument 510 /errors/maxconexceeded.html
BandWidthError 510
注意:有时候自订错误编码可能会