Apache Simple Method for Restricting Server Bandwidth and Connection Count

user:visitors Date:2025-05-19 00:12:2480

When managing a web server, limiting the bandwidth of the web page and the number of connections is the key to ensuring server stability. So how can you optimize your server to prevent excessive bandwidth usage or too many processes accessed at one time?

Here is an example of Apache:


1. Limit the number of connections

1.1 Use MaxClients or MaxRequestWorkers


The MaxClients (in Apache 2.4 and earlier) or MaxRequestWorkers (in Apache 2.4) parameters are used to set the maximum number of requests that the server can handle at the same time.


Set in the main configuration file (usually httpd.conf):


<IfModule mpm_prefork_module>

MaxClients 50 # for prefork MPM

</IfModule>


<IfModule mpm_worker_module>

MaxRequestWorkers 150 # for worker MPM

</IfModule>

Note: These settings depend on the Multiplexing Module (MPM) you are using, such as prefork, worker or event.


1.2 Using the mod_evasive Module


mod_evasive is a module specifically designed to defend against DoS and DDoS attacks. It does this by limiting the number of concurrent connections for a single IP address.


First make sure you have mod_evasive installed, then enable and configure it in your configuration file:


LoadModule evasive24_module modules/mod_evasive24.so


<IfModule mod_evasive24.c>

DOSHashTableSize 3097

DOSPageCount 2

DOSSiteCount 50

DOSPageInterval 1

DOSSiteInterval 1

DOSBlockingPeriod 10

</IfModule>

2. Throttle bandwidth

2.1 Using the mod_bw Module


mod_bw is an open-source Apache module that allows administrators to limit bandwidth based on IP address, time, URL, etc.


After installing the mod_bw module, enable and configure in the configuration file:


LoadModule bw_module modules/mod_bw.so


<IfModule mod_bw.c>

BWLimit 100 # is globally limited to 100 KB/s

BWClass 100# creates a class with a limit of 100 KB/s

BWClassSet 100 .gif .jpg .png # limits image files to 100 KB/s

BWClassSet 100 /downloads/ # limits all files in the /downloads/ directory to 100 KB/s

</IfModule>

2.2 Use a .htaccess file for simple bandwidth throttling


If you don't have direct access to the main profile, you can use the mod_bw module via the .htaccess file to throttle bandwidth.


Example:


<IfModule mod_bw.c>

BWLimit 100# is limited to 100 KB/s

</IfModule>

2.3 Using the mod_ratelimit Module


mod_ratelimit is another module that can be used to throttle bandwidth.


Once the mod_ratelimit module is installed and enabled, add the following to the configuration file:


<IfModule mod_ratelimit.c>

SetOutputFilter RATE_LIMIT

RateLimit 100 # is limited to 100 KB/s

RateLimitNoDelay On # Start throttling immediately

</IfModule>

2.4 Using the mod_qos Module


mod_qos is a powerful module that can be used to limit bandwidth, control the number of concurrent connections, and optimize server performance.


After installing and enabling the mod_qos module, add the following to the configuration file:


<IfModule mod_qos.c>

QS_ClientEntries 1000 # Sets the number of client entries

QS_SrvMaxConnPerIP 10 # Maximum number of connections per IP

QS_SrvMaxConnClose # Close the connection when the maximum number of connections is reached

QS_SrvMaxConnPerVHost 50 # Maximum number of connections per virtual host

QS_SrvMaxConnRatePerSec 10 # maximum connection rate per second

QS_SrvMaxConnBurst 50 # Maximum number of burstable connections

QS_SrvMaxConnCloseOnBurst # Close the connection when the maximum burst number of connections is reached

QS_SrvMinDataRate 1000 # minimum data transfer rate

QS_SrvMaxDataRate 10000 # maximum data transfer rate

QS_SrvMaxDataRateBurst 5000 # Maximum data burst rate

</IfModule>

With the above methods, you can easily limit the bandwidth and number of connections on the Apache server, thus improving the stability of the server.

Popular articles
latest article