Apache concurrent connections and bandwidth control in Linux
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 redhatlinux
9.0 + Apache 1.3.9 to describe how to use them.
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 http://dominia.org/djao/limitipconn.html, which is used by the user
Version 1.3.9.
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 and run wget logs on the server.
The zxvfmod_limitipconn-0.04.tar.gz decompress the tar package, generates the mod_limitipconn-0.04 directory under the current directory, and the cdmod_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 whereisapxs command, we can determine the path of the apxs command. For example, the path of my apxs command is/usr/local/Apache/bin/apxs, enter/usr/local/Apache/bin/apxs-c-I-a mod_limitipconn.c to compile mod_limitipconn.c. This command will be automatically run in 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/local/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 does not run properly. Therefore, move the two lines to the corresponding row that does not exist, and then confirm that the mod_status module has been loaded, the extendedstatus on line is added 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
<Ifmodule mod_limitipconn.c>
<Location/> # 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
</Location>
<Location/MP3> # 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.
</Location>
</Ifmodule>
When global restrictions are imposed, place the code in httpd. the conf file does not contain virtualhost. If you want to restrict a virtual host, place it between and. You can change the location and maxconnperip to conveniently 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.9 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
Libexec/Apache/mod_bandwidth.so
Addmodule mod_bandwidth.c and the two rows must be added at the beginning of the corresponding region so that the module runs at the lowest priority.
After confirming, enter wget requests
-C mod_bandwidth.c-O/usr/local/Apache (Apache Module Directory). The compilation program automatically puts the compiled mod_bandwidth.so file to the Apache Module Directory, 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.
<Ifmodule mod_bandwidth.c>
Bandwidthdatadir "/tmp/apachebw /"
Bandwidthmodule on
</Ifmodule>
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
<Directory/home/www/thinkjam/download/soft>
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.
</Directory>
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.