# Multiple limit\_conn per connection

**URL:** <https://www.ruby-forum.com/t/multiple-limit-conn-per-connection/198080>\
**Category:** NGINX\
**Created:** [October 26, 2010, 5:07am UTC](https://www.ruby-forum.com/t/multiple-limit-conn-per-connection/198080 "2010-10-26T05:07:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 26, 2010, 5:07am UTC](https://www.ruby-forum.com/t/multiple-limit-conn-per-connection/198080/1 "2010-10-26T05:07:53Z")

</div>

I’m trying to limit the number of simultaneous connections based on a  
CGI args. For example some queries are more expensive to process than  
others, and I’m trying to avoid a few slow/expensive queries to starve  
out all the other faster queries.

I would like to set an overall (server wide) connection limit of 100  
connections overall, with 50 connection limit for &opt1 is set, a 30  
connection limit if &opt2 is set, and a 10 connection limit of &opt3 is  
set.

Ideally I could use “if” but of course “if is evil” so something I’m  
trying something like this:

# invariant variable so that all connections are in the same traffic

bucket  
limit\_zone all $nginx\_version 64k;  
limit\_zone opt1 $nginx\_version 64k;  
limit\_zone opt2 $nginx\_version 64k;  
limit\_zone opt3 $nginx\_version 64k;

…

location / {  
limit\_conn all 100;

if ( $arg\_opt1 ) {  
rewrite ^ /opt1/$request\_uri last;  
}  
proxy\_pass [http://backends](http://backends);  
}

location /opt1/ {  
limit\_conn opt1 50;  
if ( $arg\_opt2 ) {  
rewrite ^ /opt2/$request\_uri last;  
}  
proxy\_pass [http://backends](http://backends);  
}

location /opt2/ {  
limit\_conn opt2 30;  
if ( $arg\_opt3 ) {  
rewrite ^ /opt3/$request\_uri last;  
}  
proxy\_pass [http://backends](http://backends);  
}

location /opt3/ {  
limit\_conn opt3 10;  
proxy\_pass [http://backends](http://backends);  
}

---

<div class="post-metadata">

**Author:** ![bryndole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@bryndole](https://www.ruby-forum.com/u/bryndole)\
**Post date:** [October 28, 2010, 3:51am UTC](https://www.ruby-forum.com/t/multiple-limit-conn-per-connection/198080/2 "2010-10-28T03:51:04Z")

</div>

I did some more research, ran a few experiments, and even looked at the  
code.

What I described above is not possible. The limit\_conn method fires  
exactly once for a connection, even if it goes to a “@retry” location.  
If you have several internal redirects ( rewrite /foo /bar last; ) only  
the last limit\_conn is fired.

Posted at Nginx Forum:

> **[Re: multiple limit\_conn per connection](https://forum.nginx.org/read.php?2,144501,145154#msg-145154)**
>
> I'm trying to limit the number of simultaneous connections based on a CGI args. For example some queries are more expensive to process than others, and I'm trying to avoid a few slow/expensive queries to starve out all the other faster queries. I...

---

<div class="post-metadata">

**Author:** ![bryndole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@bryndole](https://www.ruby-forum.com/u/bryndole)\
**Post date:** [October 28, 2010, 4:49am UTC](https://www.ruby-forum.com/t/multiple-limit-conn-per-connection/198080/3 "2010-10-28T04:49:29Z")

</div>

bryndole at 2010-10-28 9:49 wrote:

> I did some more research, ran a few experiments, and even looked at the  
> code.
> 
> What I described above is not possible. The limit\_conn method fires  
> exactly once for a connection, even if it goes to a “@retry” location.  
> If you have several internal redirects ( rewrite /foo /bar last; ) only  
> the last limit\_conn is fired.

Maybe you can also use the limit\_request. This module is similar with  
the limit\_conn module. Both the two module are limited to be used only  
once in one request.

> Posted at Nginx Forum:  
> [http://forum.nginx.org/read.php?2,144501,145154#msg-145154](http://forum.nginx.org/read.php?2,144501,145154#msg-145154)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Weibin Y.
