Introduction of Apache concurrent connection number and bandwidth control in Linux

Source: Internet
Author: User
Tags ftp mkdir


Linux+apache's stability, security and performance, as well as low prices are gaining a growing market share, using Linux+apache as a Web server more and more friends, and Apache as an HTTP service, compared to FTP is always not easy to control, Especially when the Web site to provide software/music downloads HTTP, if each user to open multiple threads and no bandwidth constraints, will soon reach the maximum number of HTTP connections or network congestion, so that many of the site's normal services can not run. However, Apache users have already developed the Mod_limitipconn and mod_bandwidth two modules to control the number of concurrent connections to HTTP and the bandwidth users can use, the following will be Redhat Linux 7.3+apache 1.3.7来 describes how they are used.

First, use Mod_limitipconn to limit the number of concurrent connections to Apache

Mod_limitipconn can control the number of concurrent connections for each IP address at the same time as a directory of servers, and is a very useful module whose official web page is http://dominia.org/djao/limitipconn.html and the latest version for Apache 1.3.7 0.04, and there are modules to support Apache 2.x download, because I use the Apache 1.3.7 version, so please use the 2.x version of Apache friends to their official website to see the specific use of the method.

The Mod_limitipconn for Apache 1.3x offers three installation methods, namely, the tar package, the RPM installation file, and the RPM source file, since the RPM package can only be used in the RedHat 7.x version and does not support the detection proxy server. So we generally use the way the TAR package is installed.

Log on to the server as an administrator, and then run wget http://dominia.org/djao/limit/mod_limitipconn-0.04.tar.gz on the server to download Mod_limitipconn's tar packets to the server, Then mod_limitipconn-0.04.tar.gz the tar package by running the tar zxvf again, the mod_limitipconn-0.04 directory is generated in the current directory, and then the CD Mod_ limitipconn-0.04 into this directory, the next step is to use APXS to compile the mod_limitipconn.c in the directory. At this point, we need to make sure that our Apache is installed in that directory and find out where the APXS command is placed.

With the command Whereis APXS, we can determine the path to the APXS command, as my APXS command is/USR/SBIN/APXS, then enter/usr/sbin/apxs-c-i-a mod_limitipconn.c to Mod_ LIMITIPCONN.C, this command automatically adds the required information to your Apache configuration file httpd.conf and copies the generated mod_limitipconn.so modules to the Apache module directory. However, to confirm that this command is working properly, first check your Apache module directory (mine is/usr/lib/apache) to see if the internal contains mod_limitipconn.so files, if not, please mod_ The files generated in the limitipconn-0.04 directory are copied here.

The httpd.conf that was automatically generated by the command just now might be a bit of a mistake, and in my system it will loadmodule Limitipconn_module modules/mod_limitipconn.so on the

LoadModule Python_module modules/mod_python.so

Between, while the addmodule mod_limitipconn.c placed in the

Addmodule MOD_PYTHON.C

, which directly causes the Mod_limitipconn module to not function correctly, move the two lines to the line that does not have them, and then verify that the Mod_status module is loaded and that the extendedstatus on this line is added under Mod_status. Then our Mod_limitipconn module is installed, and the next step is to set the number of concurrent connections to a directory.

Mod_limitipconn can have different restrictions on global and virtual hosts, and their syntax structure is

#所限制的目录所在, which represents the root directory of the host
Maxconnperip 3 #所限制的每个IP并发连接数为3个
Noiplimit image/* #对图片不做IP限制

#所限制的目录所在, the/mp3 directory of the host is represented here
Maxconnperip 1 #所限制的每个IP并发连接数为1个
Onlyiplimit audio/mpeg Video #该限制只对视频和音频格式的文件

When you limit the global, place this code where there is no virtualhost in the httpd.conf file, if you limit a virtual host, put it between and, we can change location and Maxconnperip convenient control of the number of directories and concurrent connections that are limited.

Finally, as long as the Apache service is restarted, the limit of the number of concurrent connections can take effect.

Second, use Mod_bandwidth to control the Apache bandwidth

Apache 1.3.7 actually with mod_bandwidth support, just do not have this module so file, we do is to download the Mod_bandwidth source file for compilation, and Mod_bandwidth to the corresponding settings.

Before downloading, make sure your Apache profile httpd.conf contains

LoadModule Bandwidth_module modules/mod_bandwidth.so

And

Addmodule mod_bandwidth.c

If not, please add

LoadModule Bandwidth_module
Libexec/apache/mod_bandwidth.so
Addmodule mod_bandwidth.c

And the two lines must be added to the front of the corresponding area separately, making the module run at the lowest priority level. (But 1.3.7 of Apache should have, hehe).

When you are sure, enter wget ftp://ftp.cohprog.com/pub/apache/module/1.3.0/mod_bandwidth.c download the source files to the server, and then compile them using APXS, compile methods and Mod_ Limitipconn is basically the same as I input/usr/sbin/apxs-c mod_bandwidth.c-o/usr/lib/apache (Apache module directory), the compiler will automatically compile the successful Mod_ bandwidth.so file into the Apache module directory, you can also confirm yourself, if not normal, copy the past can be.

The Mod_bandwidth runtime requires some specific directories, and by default, run the following command to create and change permissions for the directory:

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

Bandwidthdatadir "/tmp/apachebw/"
Bandwidthmodule on

At this point, we will be able to the need to limit the bandwidth of the directory to make the appropriate settings, where the directory please use the absolute path of the server. If we want to limit the download speed of the server/home/www/thinkjam/download/soft directory, that is to limit the download speed of the software under the Http://download.thinkjam.org/soft directory, Add the following to the httpd.conf file

Bandwidth thinkjam.org 0 #来自thinkjam. org downloads are not subject to speed limits
Bandwidth 210.51.21 0 #来自210.51.21 Network segment downloads are not subject to speed limits
Bandwidth all 327680 #来自其它网段的速度都限制为327680Byte, i.e. 30kb/s

Once setup is complete, restart the Apache service to take effect.

Mod_bandwidth also has a number of other useful parameters, such as adding maxconnection 120 in the middle to limit the maximum number of connections to a directory, and rejecting a new connection when the number of connections is exceeded, this parameter is associated with the Mod_ Limitipconn modules combine to control the maximum number of connections for a directory.

Other parameters please friends to their official website http://www.cohprog.com/v3/bandwidth/doc-en.html to see the relevant documents.

Apache function is really powerful, many functions can be implemented 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.

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.