The method of restricting IP concurrency and traffic control in Apache _linux

Source: Internet
Author: User
Tags bz2 time limit

This article to give you a summary of Apache limit the number of IP connections and IP concurrency and traffic control implementation methods, if only limited connection data we can directly use limit, if you want to limit the other need to use other Third-party modules

Limit the number of IP concurrent connections using the Mod_limitipconn module
Installation:

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
/usr/local/apache2/bin/apxs-c-I. MOD_LIMITIPCONN.C

Edit httpd.conf

Extendedstatus on
LoadModule limitipconn_module modules/mod_limitipconn.so
 
<ifmodule mod_limitipconn.c >
<location/>                       #对应根目录
   maxconnperip 5                 # Maximum concurrency number is 5
   noiplimit image/*                # no restrictions on pictures
</location>
<location/test>                    # Restrictions on the test directory under the root directory
   maxconnperip 2                  # maximum concurrency number is 2
</location>
</ifModule>

If you want to limit the number of IP concurrent connections for a virtual host, you can modify the extra/httpd-vhost.conf to Ifmodule mod_limitipconn.c This section of the copy to VirtualHost.

Test:
Test is very simple, the maximum number of connections to a smaller, any browser to access the page, and then press the F5, so that the page is constantly refreshed, you can pop up 503 errors

Limiting bandwidth using the Mod_bandwidth module
the Mod_bandwidth can control the concurrent number of IP, can also control the download traffic, and also can control the traffic of a directory.
Installation:

wget http://bwmod.sourceforge.net/files/mod_bw-0.7.tgz
tar zxvf mod_bw-0.7.tgz
cd MOD_BW
/usr/local/ Apache2/bin/apxs-c-I. MOD_BW.C

To edit the httpd.conf configuration file:

LoadModule bw_module modules/mod_bw.so
 
<virtualhost *:80> ServerName
    DocumentRoot  /var/www/vhost1
    bandwidthmodule on
    forcebandwidthmodule in
    bandwidth all 1024000
    Minbandwidth all 50000     # per client maximum speed up to 50KB
    Largefilelimit * 50000
    maxconnection all 6
< /virtualhost>

Other parameter case description, detailed see Mod_bw.txt in the source package:

Bandwidth localhost 0                   #对localhost不限速
bandwidth 192.168.1.5 102400            #对192.168.1.5 speed limit 100KB
bandwidth " U:^mozilla (. *) "10240       #用mozilla时限速10KB
Bandwidth" U:wget "102400           # if used wget download time limit 10KB
minbandwidth ALL-1                   # ensures that each client has a maximum speed of up to 10KB
largefilelimit. jpg 10240           #jpg file is over 100KB, speed limit 10KB
# below 510 pretty good, if not set, Apache itself will report an error, on the root of the 404 almost, the page is very ugly
errordocument 510/exceed_speed.html
bandwidtherror 510
maxconnection All                    #所有ip最大连接数为10
maxconnection 192.168.1.5 5             #192.168.1.5 Maximum number of connections is 5 

Test:
Before using the Mod_bw.so module:

After using the Mod_bw.so module:

Issues that may occur during the installation:

1, add complete mod_bw.so module, restart Apache error:

[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]

Modify the MOD_BW.C to:

#ifdef apr_major_version  //Add this line
#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

Recompile:

/usr/local/apache2/bin/apxs-c-I. MOD_BW.C

2. If the server does not have APXS installed, you can install it by reference to the following methods:


Apxs is a tool for compiling and installing extension modules for Apache HTTP servers to compile one or more source or target code files as dynamic shared objects that can be loaded into the Apache server at run time with the LoadModule instructions provided by MOD_SO.

Enter the command to see if there is a httpd-devel this package if there is no need to install

Rpm-qa|grep httpd
yum-y Install Httpd-devel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.