Apache concurrent connections and bandwidth control in Linux

Source: Internet
Author: User
Article title: Apache concurrent connections and bandwidth control in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
The stability, security, performance, and low price of Linux + Apache are gaining more and more market share. more and more friends are using Linux + Apache as website servers, while Apache is an http service, it is not easy to control over FTP, especially when the website provides software/music downloads via http, if each user enables multiple threads without bandwidth restrictions, it will soon reach the maximum number of http connections or cause network congestion, making many normal services of the website unable to run. However, Apache users have already developed mod_limitipconn and mod_bandwidth modules to control the number of concurrent http connections and the bandwidth available to users, the following describes how to use RedHat Linux 7.3 + Apache 1.3.7.
  
   I. use mod_limitipconn to limit the number of concurrent connections of Apache
Mod_limitipconn can control the number of concurrent connections from each IP address to a directory on the server at the same time. it is a very useful module. its official webpage is 0.04 of Apache 1.3.7 and supports Apache 2. x module download, because I use Apache 1.3.7, so please use 2. A friend of Apache of version x goes to the official website to check the specific usage method.
  
Mod_limitipconn for Apache 1.3x provides three installation methods: tar package, rpm installation file, and rpm source file. since the rpm package can only be used in RedHat 7.x, and does not support proxy detection, therefore, we generally use the tar package installation method.
  
Log on to the server as an administrator, then run wget javaszxvf mod_limitipconn-0.04.tar.gz on the server to decompress the tar package, generate the mod_limitipconn-0.04 directory under the current directory, and then the cd mod_limitipconn-0.04 enters this directory, the next step is to use apxs to compile mod_limitipconn.c in the directory. At this time, we need to determine the directory where our Apache is installed and find out where the apxs command is.
  
By running the whereis apxs command, we can determine the path of the apxs command. for example, if my apxs command is/usr/sbin/apxs, then, enter/usr/sbin/apxs-c-I-a mod_limitipconn.c to compile mod_limitipconn.c. this command will automatically go to your Apache configuration file httpd. add the required information to the conf file and copy the generated mod_limitipconn.so module to the module directory of Apache. To check whether the command works properly, check the directory of your Apache Module (my directory is/usr/lib/apache) to check whether the mod_limitipconn.so file is contained in the directory, if not, copy the files generated in the mod_limitipconn-0.04 directory here.
  
The httpd. conf generated by the command just now may have some errors. in my system, it puts LoadModule limitipconn_module modules/mod_limitipconn.so
  
    
LoadModule python_module modules/mod_python.so
    
  
And put AddModule mod_limitipconn.c in
  
    
AddModule mod_python.c
    
  
The mod_limitipconn module cannot run normally. therefore, move the two lines to And then confirm that the mod_status module has been loaded, and added the ExtendedStatus On line under mod_status. In this case, the mod_limitipconn module is installed. The next step is to set the number of concurrent connections for a directory.
  
Mod_limitipconn can impose different restrictions on Global and virtual hosts. Its syntax structure is
  
    
       # The restricted directory, which indicates the root directory of the host
MaxConnPerIP 3 # The number of concurrent connections per IP address is 3
NoIPLimit image/* # no IP address limit on images
      
       # The restricted directory, which indicates the/mp3 Directory of the host.
MaxConnPerIP 1 # The number of concurrent connections per IP address is 1
OnlyIPLimit audio/mpeg video # this restriction only applies to video and audio files.
      
    
  
When global restrictions are imposed, place the code in the httpd. conf file without VirtualHost. if you restrict a virtual host, place it in And By changing the Location and MaxConnPerIP, we can easily control the restricted directory and number of concurrent connections.
  
Finally, as long as you restart the Apache service, the limit on the number of concurrent connections takes effect.
  
   II. use mod_bandwidth to control Apache bandwidth
Apache 1.3.7 is actually supported by mod_bandwidth, but does not have the so file of this module. what we do is to download the source file of mod_bandwidth for compilation and set it accordingly.
  
Before downloading, check whether your Apache configuration file httpd. conf contains
  
    
LoadModule bandwidth_module modules/mod_bandwidth.so
    
  
And
  
    
AddModule mod_bandwidth.c
    
  
If not, add
  
LoadModule bandwidth_module modules/mod_bandwidth.so
AddModule mod_bandwidth.c
  
In addition, these two lines must be added at the beginning of the corresponding region so that the module runs at the lowest priority. (However, Apache 1.3.7 should have one, huh, huh ).
  
After confirmation, enter wget modules-c mod_bandwidth.c-o/usr/lib/apache (Apache module directory ), the compiled program automatically puts the successfully compiled mod_bandwidth.so file in the module directory of Apache. you can also confirm it by yourself. if it is abnormal, copy it.
  
Some specific directories are required when mod_bandwidth is run. by default, run the following command to create and change the directory permissions:
  
Mkdir/tmp/apachebw
Mkdir/tmp/apachebw/link
Mkdir/tmp/apachebw/master
Chmod-R 777/tmp/apachebw
  
Then open the httpd. conf file and add the following content.
  
    
BandWidthDataDir "/tmp/apachebw /"
BandWidthModule on
    
  
In this case, you can set the directory for which you want to limit the bandwidth. use the absolute path of the server for the directory here. If we want to limit the download speed of the server/home/www/softicp/download/soft Directory, that is, to limit the download speed of the software under the website http://download.softicp.com/softprogram, the content of the httpd.conf file will be increased
  
    
BandWidth cbifamily.com 0 # downloading from cbifamily.com is not subject to speed restrictions
BandWidth 210.51.21 0 # downloading from the 210.51.21 network segment is not subject to speed restrictions
BandWidth all 327680 # the speed from other network segments is limited to 327680 bytes, that is, 30KB/s.
    
  
After the configuration is complete, restart the Apache service to take effect.
  
Mod_bandwidth has many other useful parameters, such Adding MaxConnection 120 in the middle can limit the maximum number of connections in a directory. when the number of connections exceeds the specified number of connections, a new connection is rejected. this parameter can be combined with the mod_limitipconn module to control the maximum number of connections in a directory.

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.