# Limiting upstream backend connections

**URL:** <https://www.ruby-forum.com/t/limiting-upstream-backend-connections/219290>\
**Category:** NGINX\
**Created:** [April 27, 2012, 2:33am UTC](https://www.ruby-forum.com/t/limiting-upstream-backend-connections/219290 "2012-04-27T02:33:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [April 27, 2012, 2:33am UTC](https://www.ruby-forum.com/t/limiting-upstream-backend-connections/219290/1 "2012-04-27T02:33:28Z")

</div>

I am hosting a CPU intensive service and use nginx as a reverse proxy  
cache / load balancer to my backend(s). Basic config looks like:

upstream myservice {  
server 127.0.0.1:80;  
server 123.123.123.123:80;  
}

limit\_req\_zone $binary\_remote\_addr zone=softlimit:10m rate=1r/s;

location /R {  
proxy\_pass [http://myservice/R](http://myservice/R);  
proxy\_cache mycache;  
proxy\_cache\_methods POST;  
proxy\_cache\_key “$request\_uri|$request\_body”;  
limit\_req zone=softlimit burst=10;  
}

So I use limit\_req to make sure that no single user can do more than 1  
req per sec to my service.

However, actually more important is that the upstream backend servers  
are protected against overload. Some of the requests might take 30 or  
60 seconds to return. I want to limit the total number of active  
concurrent connections to the upstream back-end at, say, 10. Requests  
that hit the cache and never make it to the backend should not be  
counted towards this. Only active backend connections.

I had a look at limit\_conn, but it looks like this limits the client  
connections rather than the back-end connections, is that correct?

Thanks for advice,

Jeroen

---

<div class="post-metadata">

**Author:** ![Jeroen\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@Jeroen\_O](https://www.ruby-forum.com/u/Jeroen_O)\
**Post date:** [April 27, 2012, 3:19pm UTC](https://www.ruby-forum.com/t/limiting-upstream-backend-connections/219290/2 "2012-04-27T15:19:06Z")

</div>

I think this module will fit your demands:

> **[GitHub - cfsego/nginx-limit-upstream: limit the number of connections to...](https://github.com/cfsego/nginx-limit-upstream)**
>
> limit the number of connections to upstream for NGINX - GitHub - cfsego/nginx-limit-upstream: limit the number of connections to upstream for NGINX

Sunny Chen

-----ʼԭ-----  
: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] Jeroen O.  
ʱ: 2012427 8:33  
ռ: removed\_email\_address@domain.invalid  
: limiting upstream backend connections

I am hosting a CPU intensive service and use nginx as a reverse proxy  
cache / load balancer to my backend(s). Basic config looks like:

upstream myservice {  
server 127.0.0.1:80;  
server 123.123.123.123:80;  
}

limit\_req\_zone $binary\_remote\_addr zone=softlimit:10m rate=1r/s;

location /R {  
proxy\_pass [http://myservice/R](http://myservice/R);  
proxy\_cache mycache;  
proxy\_cache\_methods POST;  
proxy\_cache\_key “$request\_uri|$request\_body”;  
limit\_req zone=softlimit burst=10;  
}

So I use limit\_req to make sure that no single user can do more than 1  
req per sec to my service.

However, actually more important is that the upstream backend servers  
are protected against overload. Some of the requests might take 30 or  
60 seconds to return. I want to limit the total number of active  
concurrent connections to the upstream back-end at, say, 10. Requests  
that hit the cache and never make it to the backend should not be  
counted towards this. Only active backend connections.

I had a look at limit\_conn, but it looks like this limits the client  
connections rather than the back-end connections, is that correct?

Thanks for advice,

Jeroen

* * *

nginx mailing list  
removed\_email\_address@domain.invalid  
[http://mailman.nginx.org/mailman/listinfo/nginx](http://mailman.nginx.org/mailman/listinfo/nginx)

This email (including any attachments) is confidential and may be  
legally privileged. If you received this email in error, please delete  
it immediately and do not copy it or use it for any purpose or disclose  
its contents to any other person. Thank you.

(κθ)ܺлϲܷɱȷռˣɾʼ벻ҪʽиƲκ;͸¶ʼ֮ݡлл
