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 the wget http://dominia.org/djao/limit/mod_limitipconn-0.04.tar.gz on the server to download the tar package of mod_limitipconn to the server, and then press run tar zxvf mod_limitipconn-0.04.tar.gz to decompress the tar package, the mod_limitipconn-0.04 directory is generated 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
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 the wget ftp://ftp.cohprog.com/pub/apache/module/1.3.0/mod_bandwidth.c to download the source file to the server, and then use apxs to compile it, the compilation method is basically the same as mod_limitipconn, for example, if I enter/usr/sbin/apxs-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/home/www/thinkjam/download/soft directory on the server, that is, to limit the download speed of software under the http://download.thinkjam.org/softdirectory, the content of the httpd.conffile will be increased
BandWidth thinkjam.org 0 # downloading from thinkjam.org 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 also has many other useful parameters. For example, adding MaxConnection 120 in the middle can limit the maximum number of connections in a directory. When the specified number of connections is exceeded, a new connection is rejected, this parameter can be combined with the mod_limitipconn module to control the maximum number of connected persons in a directory.
Other parameters ask friends to visit their official website http://www.cohprog.com/v3/bandwidth/doc-en.html to view the relevant documentation.
Apache functions are indeed powerful, a lot of functions can be achieved by adding modules, In the http://modules.apache.org/can find more modules, we can also write their own modules to achieve the corresponding functions.
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.