Anton
Anton

Reputation: 317

Nginx: Limit number of simultaneous connections per IP to backend

We use nginx with an application server as a backend.

We need to limit number of simultaneous connections per IP to backend. We used limit_conn nginx directive for this purpose. But it doesn't work well in all cases. If user generates a lot of connections from one IP and quickly closes them, then nginx passes this request to a backend, but because client connection is already closed, this connection is not count in limit_conn.

Is it possible to limit number of simultaneous connections per IP to backend server with nginx?

Upvotes: 6

Views: 9799

Answers (2)

hostmaster
hostmaster

Reputation: 2150

I'm afraid this facility is not yet available for nginx out of the box. According to the Nginx FAQ

Many users have requested that Nginx implement a feature in the load balancer to limit the number of requests per backend (usually to one). While support for this is planned, it's worth mentioning that demand for this feature is rooted in misbehaviour on the part of the application being proxied

I've seen some 3rd parties module for that nginx-limit-upstream but I've never tried.

Upvotes: 0

Alexander Azarov
Alexander Azarov

Reputation: 13221

You may want to set

proxy_ignore_client_abort off;

Determines should the connection with a proxied server be closed if a client closes a connection without waiting for a response.

from the documentation

Another suggestion is to use limit_req to limit the request rate.

Upvotes: 4

Related Questions