Nginx · The edge of your stack
How do you limit concurrent connections per client?
Short answer
Define a limit_conn_zone keyed by client and apply limit_conn in a server or location. It caps simultaneous connections, which is different from limiting request rate.
Connection limiting protects against clients holding many sockets open, such as slow downloaders or abandoned connections. It does not directly limit how many requests a client sends over one connection.
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn_zone $server_name zone=per_server:10m;
server {
limit_conn addr 10;
limit_conn per_server 1000;
limit_conn_status 429;
location /downloads/ {
limit_conn addr 2;
limit_rate 2m;
}
}How it differs from rate limiting
- limit_conn caps simultaneous connections; limit_req caps requests over time
- HTTP/2 multiplexes many streams over one connection, so connection limits are weaker there
- limit_rate caps bandwidth per response, not request count
- Zones use shared memory, so the limit applies across workers
- A low limit can break browsers that open several connections to the same host
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between limit_conn and limit_req?
- 2Why is limit_conn less effective for HTTP/2 clients?
- 3What does limit_rate do?
- 4How do shared memory zones coordinate limits across workers?
Say it in the interview
I use limit_conn_zone keyed by client and apply limit_conn to cap simultaneous connections. It protects against slow clients holding sockets, while limit_req protects against high request rates. I also use limit_rate to cap bandwidth for downloads. I keep the connection cap above normal browser parallelism, and I account for HTTP/2 multiplexing when interpreting the result.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Nginx mock interview and get scored feedback on depth, clarity and confidence.