Nginx bandwidth limit controls how fast data is transferred to clients, preventing any single user from saturating your server’s connection. You set it with the limit_rate directive (per-connection) and limit_rate_after (initial burst), or with the ngx_http_core_module‘s limit_rate inside location, server, or http blocks. This article explains the directives, how to combine them with limit_conn and limit_req, and common pitfalls.
What Does Nginx Bandwidth Limit Actually Do?
Nginx’s limit_rate sets a maximum transfer rate for a single connection, in bytes per second (default is 0, meaning unlimited). The rate applies to the response body, not headers, and is enforced per connection, not per IP or per user. If a client opens multiple connections (e.g., a download manager), each connection gets its own limit, so total bandwidth can multiply. To cap a client’s total, you must combine limit_rate with limit_conn (connection limit) or use a proxy-level rate limiter.
The directive accepts sizes like 100k (kilobytes) or 1m (megabytes), but note that Nginx treats k as 1024 bytes and m as 1024*1024 bytes. For precise control, you can also use limit_rate_after to allow a certain amount of data to be sent at full speed before throttling begins. This is useful for web pages where you want the HTML and CSS to load instantly but throttle large downloads.
How to Configure limit_rate in Nginx
Place limit_rate in the http, server, or location block. The most specific block wins. For example, to limit all downloads from /downloads/ to 500 KB/s after the first 1 MB:
location /downloads/ {
limit_rate_after 1m;
limit_rate 500k;
}
To apply a global limit to all responses, put it in the http block:
http {
limit_rate 200k;
}
You can also set it dynamically using variables, like $limit_rate, which allows per-request rates based on map or geo. For example, give premium users a higher rate:
map $remote_addr $rate {
default 100k;
192.168.1.0/24 1m;
}
server {
set $limit_rate $rate;
}
Note that set $limit_rate inside a server block overrides static limit_rate for that server.
Combining with Connection and Request Limits
Bandwidth limiting alone doesn’t prevent a client from opening many connections. Use limit_conn to cap the number of simultaneous connections per IP, and limit_req to cap request rate. For example:
http {
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=req:10m rate=5r/s;
server {
location /downloads/ {
limit_conn addr 2;
limit_req zone=req burst=10;
limit_rate 500k;
}
}
}
This limits each IP to 2 concurrent connections, 5 requests per second (with burst of 10), and each connection to 500 KB/s. The total per-IP bandwidth is then at most 1 MB/s (2 connections × 500 KB/s).
How to Choose the Right Rate Limit
Choosing a rate depends on your server’s uplink, the typical file size, and user expectations. A few practical guidelines:
- For a small VPS with 100 Mbps (12.5 MB/s) uplink, setting a per-connection limit of 1-2 MB/s prevents one user from hogging the pipe.
- For video streaming, you might need 1-5 Mbps per stream; check your content’s bitrate and set limit_rate slightly above that.
- For API responses (JSON), limit_rate is rarely needed; use
limit_reqinstead. - Use
limit_rate_afterto allow small assets to load instantly; set it to the average page size (e.g., 1-2 MB).
Remember that limit_rate is per connection, so if you don’t limit connections, a client can bypass your bandwidth cap with multiple threads. Always test with a real download tool after configuring.
Common Problems and Misconceptions
One common mistake is thinking limit_rate applies to uploads. It only limits download speed (response body). For uploads, you need a different module like client_max_body_size (which limits size, not speed) or a proxy that throttles uploads, which Nginx doesn’t do natively.
Another pitfall: limit_rate is enforced per connection, but HTTP/2 multiplexes multiple streams over one connection. In practice, Nginx applies limit_rate to the whole connection, so all streams share the same rate. This is usually fine, but be aware that HTTP/2 connections may be slower per stream if you set a low limit.
Also, limit_rate does not apply to responses from a proxy if the proxy sends data faster than the limit? Actually, Nginx buffers and then sends at the limited rate, so it works, but it may increase memory usage for large files. To avoid buffering, you can set proxy_buffering off;, but then Nginx will still throttle, just without buffering.
Finally, a common misconception: setting limit_rate in http block affects all servers, including localhost. If you’re testing, you might be surprised by slow local responses. Use limit_rate 0; in a specific server to override.
Nginx vs. Other Tools (Apache, Cloudflare, OS)
Apache has a similar module mod_ratelimit with SetOutputFilter RATE_LIMIT and SetEnv rate-limit 100, but it’s less flexible and doesn’t have limit_rate_after equivalent. Cloudflare and other CDNs offer bandwidth limiting at the edge, which can protect origin servers, but they typically limit per IP or per plan, not per connection. OS-level tools like tc (traffic control) can shape traffic globally, but they require root and are more complex. Nginx’s built-in limit is the simplest for per-URI control.
Costs and Performance Impact
Using limit_rate has negligible CPU overhead because it’s just a timer. However, if you set a very low limit and have many concurrent connections, Nginx may hold many connections open longer, increasing memory usage (each connection uses a small buffer). Also, if you use limit_rate_after with large files, Nginx will buffer the first chunk, which can cause disk I/O. In practice, the impact is minimal for typical web servers.
Safety and Fair Use Considerations
Bandwidth limiting is a fair-use measure, not a security feature. It doesn’t prevent DDoS attacks; for that you need rate limiting (limit_req) and connection limits. Also, be careful not to set limits too low for legitimate users, especially on mobile networks where TCP congestion control may already slow down. Always test with real browsers and download managers.
If you’re serving large files, consider using X-Accel-Redirect with internal locations to offload the actual file serving to Nginx directly, which is more efficient than proxying through an app server. Combine that with limit_rate for a robust solution.
Frequently Asked Questions
Does limit_rate apply to uploads?
No, limit_rate only limits the download speed (response body). Nginx does not have a built-in directive to limit upload speed. To limit upload size, use client_max_body_size, but for upload speed, you’d need a reverse proxy like HAProxy or an application-level throttle.
Can I set different rates for different file types?
Yes, use a location block with a regex or prefix match. For example, location ~* \.(mp4|zip)$ { limit_rate 1m; } limits only those extensions. Alternatively, use a map based on $uri to set $limit_rate dynamically.
Why is my download speed not matching the limit?
Several reasons: the limit is per connection, so multiple connections add up; the client may be using HTTP/2 multiplexing, which shares the limit across streams; or your server’s uplink is slower than the limit. Also, check that you didn’t set limit_rate_after to a value larger than the file size, which would allow the whole file to download at full speed.
How do I remove a bandwidth limit?
Set limit_rate 0; in the appropriate block. That’s the default value, meaning unlimited. If you set it in a parent block, you can override with limit_rate 0; in a child block.
Does limit_rate work with Nginx Plus?
Yes, limit_rate is part of the open-source Nginx core module. Nginx Plus adds additional rate-limiting features like limit_req with more options, but bandwidth limiting is the same.
Can I limit bandwidth per IP address?
Not directly with limit_rate, because it’s per connection. To limit per IP, you must combine it with limit_conn to cap connections per IP. For example, limit_conn addr 1; plus limit_rate 500k; gives each IP a maximum of 500 KB/s.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Gadgets After Support Ends: Do They Improve?
- Delete Google Search Results About You
- OnePlus Buds Pro 3 Price Drop: Are They Worth It?
- Cloud Migration Ways to Prepare
- Remote Work Productivity Hacks That Actually Work
- Epic Games Removes Dark and Darker? Fact Check