Skip to main content

Configuring Bandwidth Limiting

1. Setting Download Rate Limit

Directive Values Description
limit_rate 10k Limits the download rate to 10 kilobytes per second
limit_rate_after 1m Starts rate limiting after 1 minute of connection

Example: Configuring Download Rate Limit

location /download/ {
    limit_rate 10k;
    limit_rate_after 1m;
}

2. Using After Limit

Directive Values Description
limit_rate_after 0, 1k, 1m Specifies the amount of data to be sent before limiting the rate. Default is 0, meaning rate limiting starts immediately.

Example: Configuring limit_rate_after

location /download/ {
    limit_rate_after 1m;
    limit_rate 50k;
}
Note: The limit_rate_after directive is useful for allowing initial data transfer to proceed at full speed before applying bandwidth limits.

3. Implementing Per-connection Limits

Directive Values Description
limit_rate 10k Limits the rate of response transmission to the client in bytes per second
limit_rate_after 1m Specifies the amount of data to be sent before limiting begins

Example: Setting Per-connection Bandwidth Limits

location /download {
    limit_rate 10k;
    limit_rate_after 1m;
}
Note: The limit_rate directive applies to each client connection, and the limit_rate_after parameter allows initial data to be sent at full speed before applying the limit.

4. Using Variables for Dynamic Limits

Variable Description Usage
$limit_rate Rate limit in bytes per second limit_rate $limit_rate;
$limit_conn Connection limit per IP limit_conn $limit_conn 10;

Example: Setting Dynamic Rate Limits

server {
    listen 80;
    location / {
        limit_rate $limit_rate;
        proxy_pass http://backend;
    }
}

Example: Using Variables in Map Block

map $geoip_country_code $limit_rate {
    default 100k;
    US      200k;
    GB      300k;
}

5. Configuring Upload Limits

Directive Values Description
client_max_body_size 100m Maximum allowed size of the client request body, typically used to limit uploads.
client_body_buffer_size 128k Buffer size for reading client request body; affects how much data is read at once.

Example: Setting Upload Size Limit

location /upload {
    client_max_body_size 50m;
}
Note: Setting client_max_body_size to 0 disables the limit.
Warning: If client_max_body_size is set too low, uploads may be rejected with a 413 Request Entity Too Large error.

6. Setting Connection-based Throttling

Directive Description Values Notes
limit_conn Defines a zone for connection limiting zone=name Zone name must be defined with limit_conn_zone
limit_conn_zone Defines a shared memory zone for connection tracking zone=name:bucket_size Example: zone=conn_zone:10m
limit_conn_log_level Log level for connection limit violations info, notice, warn, error Default: error
limit_conn_status HTTP status code returned when limit is exceeded 429, 503 Default: 503

Example: Basic Connection Throttling

http {
    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
    
    server {
        listen 80;
        location / {
            limit_conn conn_zone 10;
            proxy_pass http://backend;
        }
    }
}

Example: Connection Throttling with Custom Status

http {
    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
    
    server {
        listen 80;
        location /api/ {
            limit_conn conn_zone 5;
            limit_conn_log_level warn;
            limit_conn_status 429;
            proxy_pass http://backend;
        }
    }
}
Note: The $binary_remote_addr variable is used to ensure that the IP address is stored in binary format, which is more efficient for lookups.

7. Implementing Fair Queuing

Directive Description Values Notes
limit_req Applies rate limiting to requests zone=name; burst=number; nodelay Use with limit_req_zone for fair queuing
limit_req_zone Defines a shared memory zone for rate limiting key=string; zone=name:size; rate=r/s|m Key can be $binary_remote_addr for client IP

Example: Fair Queuing Configuration

http {
    limit_req_zone $binary_remote_addr zone=api:10m rate=1r/s;

    server {
        location /api/ {
            limit_req zone=api burst=5 nodelay;
            proxy_pass http://backend;
        }
    }
}
Note: The burst parameter allows a burst of requests before rate limiting applies. nodelay prevents queuing delays for bursts.

Key Points

  • limit_req_zone creates a shared memory zone for tracking rate limits.
  • limit_req applies the rate limit to a location block.
  • Bursts can be handled with burst and nodelay for better user experience.

8. Using Map for Rate Tiers

Directive Description Values Notes
map Defines a mapping from one string to another $variable source destination Used to define rate tiers based on user-defined criteria

Example: Rate Limiting with Map

map $geoip_country_code $rate_limit {
    default      100k;
    US           500k;
    GB           300k;
}

limit_req_zone $binary_remote_addr zone=api:10m rate=$rate_limit;
Note: The map directive allows for dynamic rate limiting based on criteria like country code, which can be useful for implementing tiered bandwidth limits.

9. Monitoring Bandwidth Usage

Directive Description Values Notes
bandwidth_limit Applies bandwidth limit to a location 10m, 50k Can be set per location or server
bandwidth_limit_zone Defines a shared memory zone for bandwidth tracking name=size Used with bandwidth_limit
bandwidth_limit_rate Rate limit in bytes per second 100k, 5m Can be overridden in location blocks

Example: Setting up bandwidth tracking zone

http {
    bandwidth_limit_zone zone=bandwidth:10m;

    server {
        listen 80;
        location /download {
            bandwidth_limit zone=bandwidth;
            bandwidth_limit_rate 50k;
        }
    }
}
Note: The bandwidth_limit_zone directive requires the ngx_http_bandwidth_module to be compiled into Nginx.

Monitoring Bandwidth Usage

  • Use bandwidth_limit_zone to define shared memory zones
  • Apply bandwidth_limit to specific locations or servers
  • Set bandwidth_limit_rate to control rate limits in bytes/sec
  • Monitor bandwidth usage via logs or external tools

10. Implementing Burst Allowance

Directive Description Values Notes
limit_req Defines the rate limiting policy for a zone zone=name, rate=rate, burst=burst, nodelay Zone must be defined with limit_req_zone. Burst allows temporary excess requests.
limit_req_zone Defines a shared memory zone for rate limiting key=string, zone=name:size, rate=rate Key is used to identify the client (e.g., $binary_remote_addr). Zone size limits memory usage.

Example: Configuring Burst Allowance

http {
    limit_req_zone $binary_remote_addr zone=api:10m rate=1r/s;

    server {
        location /api/ {
            limit_req zone=api burst=5 nodelay;
            proxy_pass http://backend;
        }
    }
}
Note: The bust=5 parameter allows up to 5 excess requests before rate limiting applies. Requests beyond the burst are delayed or rejected depending on nodelay.

11. Configuring Limits per Location

Directive Values Description
limit_rate 10k Limits the rate of response transmission to client (per second)
limit_rate_after 1m Starts applying rate limit after specified amount of data has been sent

Example: Setting bandwidth limits for a location block

location /download/ {
    limit_rate 10k;
    limit_rate_after 1m;
}
Note: The limit_rate directive applies to each individual client connection, and the limit is enforced per connection.

12. Using Conditional Bandwidth Limits

Directive Description Values Notes
limit_rate_if Applies bandwidth limit based on a condition string Can use variables and conditions to control rate limiting

Example: Conditional bandwidth limit based on user agent

location /download/ {
    limit_rate_if $http_user_agent ~* "bot";
    limit_rate 10k;
}
Note: The limit_rate_if directive is available starting from Nginx 1.25.0.

Was this article helpful?