# Limit traffic rate for nginx

**URL:** <https://www.ruby-forum.com/t/limit-traffic-rate-for-nginx/200620>\
**Category:** NGINX\
**Created:** [December 17, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/limit-traffic-rate-for-nginx/200620 "2010-12-17T15:18:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigplum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bigplum](https://www.ruby-forum.com/u/bigplum)\
**Post date:** [December 17, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/limit-traffic-rate-for-nginx/200620/1 "2010-12-17T15:18:35Z")

</div>

Hello guys:

I wrote a module to limit rate by given variable rather than limit\_rate  
by connection. Most code of this module is copied from limit\_conn  
module. I’m confusing to write this new module or modify limit\_conn  
module. The modification will be easily but a new module would be  
installed easily. So, how about this new module?

> **[GitHub - bigplum/Nginx-limit-traffic-rate-module: Limiting rate by given...](https://github.com/bigplum/Nginx-limit-traffic-rate-module)**
>
> Limiting rate by given variables(like $request\_uri, $remote\_addr, etc..). - GitHub - bigplum/Nginx-limit-traffic-rate-module: Limiting rate by given variables(like $request\_uri, $remote\_addr, etc..).

README:

Nginx directive limit\_rate could limit connection’s speed, and  
limit\_conn could  
limit connection number by given variable. If the client is a browser,  
it only  
open one connection to the server. The speed will be limited to  
limit\_rate, unless  
the client is a multi-thread download tool.

The limit\_traffic\_rate module record connection numbers by the variable  
like  
limit\_conn module, and set limit\_rate to  
limit\_traffic\_rate/cur\_conn\_number. So  
if client is a browser, the download max rate will be  
limit\_traffic\_rate, if client  
is a multi-thread tool, the total rate also be limited to  
limit\_traffic\_rate.

The limit\_traffic\_rate module need to use a share memory pool. Directive  
syntax  
is same to limit\_zone.

```
http {
    limit_traffic_rate_zone rate $remote_addr 32m;

    server {
        location /download/ {
            limit_traffic_rate rate 20k;
        }
    }
}

```

Posted at Nginx Forum:

> **[\[MODULE\] limit traffic rate for nginx](https://forum.nginx.org/read.php?2,159398,159398#msg-159398)**
>
> Hello guys: I wrote a module to limit rate by given variable rather than limit\_rate by connection. Most code of this module is copied from limit\_conn module. I'm confusing to write this new module or modify limit\_conn module. The modification will be...

---

<div class="post-metadata">

**Author:** ![bigplum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bigplum](https://www.ruby-forum.com/u/bigplum)\
**Post date:** [December 17, 2010, 11:26pm UTC](https://www.ruby-forum.com/t/limit-traffic-rate-for-nginx/200620/2 "2010-12-17T23:26:59Z")

</div>

Hello!

On Fri, Dec 17, 2010 at 09:17:07AM -0500, bigplum wrote:

> Hello guys:
> 
> I wrote a module to limit rate by given variable rather than limit\_rate  
> by connection. Most code of this module is copied from limit\_conn  
> module. I’m confusing to write this new module or modify limit\_conn  
> module. The modification will be easily but a new module would be  
> installed easily. So, how about this new module?
> 
> [GitHub - Laobei2012/Nginx-limit-traffic-rate-module: Limiting rate by given variables(like $request\_uri, $remote\_addr, etc..). · GitHub](https://github.com/bigplum/Nginx-limit-traffic-rate-module)

Some notes:

1. You return NGX\_DECLINED from body filter. It’s just wrong.

2. You set r-\>limit\_rate dynamically to do actual rate limiting.  
Note that this isn’t going to work well as limit\_rate limits  
average download rate for request and not expected to be changed  
during downloading.

E.g. with 1MB/s limit per $remote\_address suppose client connects  
and starts downloading file1. After downloading 1G (at 1MB/s  
rate, i.e. after 1024 seconds) it starts downloading file2 (while  
still downloading file1). Now r-\>limit\_rate is set to 0.5MB/s for  
both requests. This is not a problem for file2 request, but file1  
request will suddenly find client was already downloaded twice as  
much as it should be allowed and will pause downloading for 1024  
seconds.

Maxim D.

---

<div class="post-metadata">

**Author:** ![bigplum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bigplum](https://www.ruby-forum.com/u/bigplum)\
**Post date:** [December 18, 2010, 2:10am UTC](https://www.ruby-forum.com/t/limit-traffic-rate-for-nginx/200620/3 "2010-12-18T02:10:03Z")

</div>

> average download rate for request and not expected  
> to be changed  
> during downloading.

Thanks a lot.

Yes, this is a problem. It will make user experience bad. I’m trying to  
fix it, but I think it’s difficult.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,159398,159538#msg-159538](http://forum.nginx.org/read.php?2,159398,159538#msg-159538)

---

<div class="post-metadata">

**Author:** ![bigplum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bigplum](https://www.ruby-forum.com/u/bigplum)\
**Post date:** [January 10, 2011, 3:32am UTC](https://www.ruby-forum.com/t/limit-traffic-rate-for-nginx/200620/4 "2011-01-10T03:32:50Z")

</div>

Hi,

I modify the calculation method. Now all the connections with same $vara

defined by “limit\_traffic\_rate\_zone” will be record in a queue. Number  
of the  
sent byte will be summarized in every cycle to get the last\_rate of  
all “same $vara” connections.

for the single connection, rate = (limit - last\_rate)/conn + last\_rate

So that, the first downloading will not be paused, but it will get a max  
speed.  
For example: The limit\_rate is 300KB.First downloading start with rate  
300KBps.  
After the second, third downloading issued, first will get 250KBps+  
rate, and the second, third will get 10~20KBps until the first finish  
its downloading.

Posted at Nginx Forum:

> **[Re: \[MODULE\] limit traffic rate for nginx](https://forum.nginx.org/read.php?2,159398,164999#msg-164999)**
>
> Hello guys: I wrote a module to limit rate by given variable rather than limit\_rate by connection. Most code of this module is copied from limit\_conn module. I'm confusing to write this new module or modify limit\_conn module. The modification will be...

---

<div class="post-metadata">

**Author:** ![bigplum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bigplum](https://www.ruby-forum.com/u/bigplum)\
**Post date:** [May 31, 2013, 11:37am UTC](https://www.ruby-forum.com/t/limit-traffic-rate-for-nginx/200620/5 "2013-05-31T11:37:24Z")

</div>

Hi guys,

Some days ago, one of this module users complaint about the unfair speed  
for  
multi-connections. So I have some time to realize a new method to limit  
download rate.

In the new code, if last second speed is lager than max rate, the timer  
is  
added in this module, and the body filter will return.  
So nothing will be sent to client, and do not use the r-\>limit\_rate in  
write\_filter to limit sendfile size.

And clcf-\>sendfile\_max\_chunk also is modified by the maximum rate.

I am not sure, is there any side-effect about this method?

> <https://github.com/bigplum/Nginx-limit-traffic-rate-module/blob/v1.0/ngx_http_limit_traffic_rate_filter_module.c>

Posted at Nginx Forum:

> **[Re: \[MODULE\] limit traffic rate for nginx](https://forum.nginx.org/read.php?2,159398,239713#msg-239713)**
>
> Hello guys: I wrote a module to limit rate by given variable rather than limit\_rate by connection. Most code of this module is copied from limit\_conn module. I'm confusing to write this new module or modify limit\_conn module. The modification will be...

---

<div class="post-metadata">

**Author:** ![bigplum](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@bigplum](https://www.ruby-forum.com/u/bigplum)\
**Post date:** [January 11, 2011, 11:37am UTC](https://www.ruby-forum.com/t/limit-traffic-rate-for-nginx/200620/6 "2011-01-11T11:37:12Z")

</div>

On 01/10/2011 03:31 AM, bigplum wrote:

> So that, the first downloading will not be paused, but it will get a max  
> speed.  
> For example: The limit\_rate is 300KB.First downloading start with rate  
> 300KBps.  
> After the second, third downloading issued, first will get 250KBps+  
> rate, and the second, third will get 10~20KBps until the first finish  
> its downloading.

I wonder if it is good or not. Would be great if it could spare limit  
fair between all connections. Anyway I am looking forward for your  
module. Thanks.

– Piotr.
