Nginx Proxy Cache – Limiting Parallel Requests to a Backend

apache-2.2load balancingnginxreverse-proxyvarnish

I am using nginx as a reverse proxy to my backend(s). Configuration is pretty basic, e.g. the core is just:

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

location / {
    proxy_pass  http://myservice;
    proxy_set_header Host myservice;
}

Now my service is computationally very heavy, and I would like nginx to limit the number of active parallel (simultaneous) requests to a single upstream backend to e.g. 10.

I looked into limit_req module, however this module only seems to focus on incoming requests per minute. I specifically would like to limit the number of active backend connections; i.e. take into account if requests have already returned or not. Is this possible at all?

In Varnish this can be done using e.g.

backend cpu1 {
  .host = "127.0.0.1";
  .port = "80";
  .max_connections = 20;
}

However I need to use nginx for this.

Best Answer

Take a look at the limit_conn module. While all the examples I've found have been on limiting by remote IP, if you pass a constant to limit_conn_zone instead of the remote IP you will be limiting by total connections to the server.

Something like this:

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

set $const 1;
limit_conn_zone $const zone=one:1m;

location / {
    limit_conn one 10;
    proxy_pass  http://myservice;
    proxy_set_header Host myservice;
}

Edit: It seems like this module is only available in more recent versions of nginx (possibly >=v1.1.8). You may need to compile from source if you want to use this option and have an older version of nginx.

Edit 2: If your nginx is only doing proxy duties, take a look at worker_connections