Skip to main content

Managing Rate Limiting and Bandwidth

1. Enabling Rate Limit Module

Enable the mod_ratelimit module to manage bandwidth and rate limiting in Apache HTTP Server.

Directive Description Default
LoadModule Loads the rate limit module mod_ratelimit_module

Example: Enable Rate Limit Module

LoadModule ratelimit_module modules/mod_ratelimit.so

2. Setting Output Rate Limits

Configure the output rate limit to control how fast data is sent to clients.

Directive Description Default
RateLimit Maximum bytes per second to send Unlimited
RateLimitBurst Maximum burst size in bytes 1024

Example: Set Output Rate Limit

<Location "/downloads">
    RateLimit 102400
    RateLimitBurst 204800
</Location>

3. Configuring Request Timeout

Set the maximum time allowed for a request to complete before being terminated.

Directive Description Default
Timeout Time in seconds for request timeout 60

Example: Configure Request Timeout

Timeout 120

4. Setting Read Timeout

Define the maximum time to wait for data to be read from a client.

Directive Description Default
KeepAliveTimeout Time in seconds to wait for a new request 5
ReadTimeout Time in seconds to wait for read operations 60

Example: Set Read Timeout

ReadTimeout 60

5. Limiting Request Body Size

Control the maximum size of request bodies to prevent abuse or resource exhaustion.

Directive Description Default
LimitRequestBody Maximum size in bytes of request body Unlimited

Example: Limit Request Body Size

<Directory "/uploads">
    LimitRequestBody 10485760
</Directory>

6. Setting Request Line Size

Define the maximum length of an HTTP request line to prevent oversized requests.

Directive Description Default
LimitRequestLine Maximum length in bytes of the request line 8190

Example: Set Request Line Size

LimitRequestLine 1024

7. Configuring Request Field Size

Set the maximum size of individual HTTP header fields to prevent oversized headers.

Directive Description Default
LimitRequestFieldSize Maximum size in bytes of a single header field 8190

Example: Configure Request Field Size

LimitRequestFieldSize 2048

8. Setting Request Fields Count

Control the maximum number of headers allowed in a request to prevent header flooding.

Directive Description Default
LimitRequestFields Maximum number of headers in a request 1000

Example: Set Request Fields Count

LimitRequestFields 500

9. Configuring Connection Limits

Manage the maximum number of concurrent connections to prevent server overload.

Directive Description Default
MaxRequestWorkers Maximum number of worker processes 150
ServerLimit Maximum number of server processes 256

Example: Configure Connection Limits

MaxRequestWorkers 200
ServerLimit 300

10. Using QoS Module

Use the Quality of Service (QoS) module to prioritize traffic and manage bandwidth.

Directive Description Default
LoadModule Loads the QoS module mod_qos_module

Example: Enable QoS Module

LoadModule qos_module modules/mod_qos.so

11. Implementing Custom Rate Limiting

Create custom rate limiting rules using mod_rewrite or mod_lua for more granular control.

Directive Description Default
RewriteRule Define custom rate limiting logic None

Example: Custom Rate Limiting with RewriteRule

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.
    RewriteRule .* - [R:429,L]
</IfModule>

12. Testing Rate Limits

Verify that rate limits are working correctly using tools like curl or ab (ApacheBench).

Example: Test Rate Limits with curl

curl -H "User-Agent: test" http://localhost/test
Note: Use tools like ab or httperf to simulate high traffic and validate rate limits.

Was this article helpful?