APACHE限制带宽

如何限制Apache的带宽流量

下载模块: ftp://ftp.cohprog.com/pub/apache/module/1.3.0/mod_bandwidth.c
官方网址: http://www.cohprog.com/mod_bandwidth.html
安装方法
1、APACI 安装
cp mod_bandwidth.c /path/to/apache/source
./configure –add-module=mod_bandwidth.c –permute-module=BEGIN:bandwidth
2、DSO方式安装
apxs -c /mod_bandwidth.c -o /path/to/apache/libexec/mod_bandwidth.so在httpd.conf加入
LoadModule bandwidth_module libexec/mod_bandwidth.so
AddModule mod_bandwidth.c

建立mod_bandwidth工作目录
mkdir -p /tmp/apachebw/link
mkdir -p /tmp/apachebw/master
chown -R nobody:nobody /tmp/apachebw
chmod -R 755 /tmp/apachebw

配置:

http://www.cohprog.com/mod_bandwidth_config
BandWidthModule On
BandWidthPulse 1000000 单位毫秒
<Directory /download>
BandWidth all 2048 单位bytes/sec #该目录下所有文件都按照2048bytes/sec速率传输
LargeFileLimit 100 1024 #大于100k的文件传输速率按1024bytes/sec
MaxConnection 1000 #最大同时连接数量
</Directory>

apache限制下载类型和实现对连接带宽的限制

附上mod_bandwidth.c的源码

 

/* ====================================================================
 * Copyright (c) 1995 The Apache Group.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *
 * 4. The names "Apache Server" and "Apache Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission.
 *
 * 5. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
 * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Group and was originally based
 * on public domain software written at the National Center for
 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
 * For more information on the Apache Group and the Apache HTTP server
 * project, please see <http://www.apache.org/>.
 *
 */

/****************************************************************************
 * Title       : Bandwidth management
 * File        : mod_bandwidth.c
 * Author      : Yann Stettler (stettler@cohprog.com)
 * Date        : 9 march 2007
 * Version     : 2.1 for Apache 1.3.29+
 *
 * Description :
 *   Provide bandwidth usage limitation either on the whole server or
 *   one a per connection basis based on the size of files, directory
 *   location or remote domain/IP.
 *
 * Revision    : 08/04/97 - "-1" value for LargeFileLimit to disable any
 *                          limits for that kind of files.
 *               01/26/98 - Include in_domain() and in_ip() in this file and
 *                          make them static so it will work with Apache 1.3x
 *               03/29/98 - Change to set bandwidth in VirtualHost directive
 *               07/17/99 - Minor changes to remove warnings at compil time
 *                          
 *                          Allow the use of the network/mask format when
 *                          setting a bandwidth for a remote host/network.
 *                          (Thanks to Sami Kuhmonen for the patch) 
 *
 *                          New directive BandWidthPulse
 *
 *               10/14/00 - Minor bug fixed
 *               12/15/00 - Bug fix when using mmap
 *               08/29/01 - Add a directive to change the data directory
 *                          (Thanks to Awesome Walrus <walrus@amur.ru> )
 *               01/12/03 - Add MaxConnection directive to limit the number
 *                          of simultaneous connections.
 *               03/09/07 - Add byte serving support and other stuff
 *
 ***************************************************************************
 * Copyright (c)1997 Yann Stettler and CohProg SaRL. All rights reserved.
 * Written for the Apache Group by :
 *
 * Yann Stettler
 * stettler@cohprog.com
 * http://www.cohprog.com/
 * http://www.animanga.com/
 *
 * Based on the original default_handler module and on bw_module 0.2 from
 * Etienne BERNARD (eb@via.ecp.fr) at VIA Centrale Reseaux, ECP, France
 * 
 * Many thanks to Colba Net Inc (Montreal) for their sponsoring of 
 * improvements to this module.
 *
 ***************************************************************************/


/*
 * Instruction :
 * -------------
 *
 * Note : this module was writen for Apache 1.3.x and tested on it
 *
 * Installation :
 *
 * 1) Insert the following line at the end of the "Configuration" file :
 *    Module bandwidth_module      mod_bandwidth.o
 *
 * WARNING : This behaviour is not the same between the various main versions
 *           of Apache. Please, read the instruction on our website for the
 *           instructions concerning the latest release : 
 *           http://www.