limit_traffic_rate
Limits the total download rate for clients based on specified variables such as their IP address or requested URL.
Description
The limit_traffic_rate directive is used to control the total download rate for clients, particularly useful in preventing excessive bandwidth usage. This directive interacts with the limit_traffic_rate_zone, which defines the rate limiting by specifying a variable (like $remote_addr or $request_uri) and the amount of shared memory to use for tracking connections.
Once the limit_traffic_rate directive is set within a context such as http, server, or location, it applies a limit to the data the client can download at any given time. It calculates the permitted download rate by distributing the total limit defined across all active connections. The formula used ensures that as multiple connections are opened, the rate limitation remains effective, enabling fair usage across different clients and connections.
To effectively implement rate limiting, the shared memory zone must be defined first using limit_traffic_rate_zone, allocating the necessary memory to track client requests. This approach allows limit_traffic_rate to maintain a consistent limit even as bandwidth fluctuates dynamically with numerous client requests.
Config Example
http {
limit_traffic_rate_zone rate $remote_addr 32m;
server {
location /download/ {
limit_traffic_rate 20k;
}
}
}Ensure the shared memory zone is defined before using the directive.
The rate must be specified in a valid unit (e.g., k, m) to avoid configuration errors.
Using a very low limit can lead to significant delays in the user's download experience.