Bird
Raised Fist0
Nginxdevops~10 mins

Connection limiting (limit_conn) in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Connection limiting (limit_conn)
Client sends request
↓
Check current connections count
↓
Is count < limit?
No→Reject connection with error
Yes↓
Allow connection
↓
Process request
↓
Connection closes, decrement count
This flow shows how nginx checks the number of active connections per key and either allows or rejects new connections based on the limit.
Execution Sample
Nginx
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn addr 2;

server {
  listen 80;
  location / {
    proxy_pass http://backend;
  }
}
This config limits each client IP to 2 simultaneous connections to the server.
Process Table
StepClient IPCurrent ConnectionsCondition (connections < 2?)ActionResult
1192.168.1.1000 < 2 = YesAllow connectionConnection accepted, count=1
2192.168.1.1011 < 2 = YesAllow connectionConnection accepted, count=2
3192.168.1.1022 < 2 = NoReject connectionConnection rejected with 503 error
4192.168.1.1100 < 2 = YesAllow connectionConnection accepted, count=1
5192.168.1.101 (after one closes)1 < 2 = YesAllow connectionConnection accepted, count=2
6192.168.1.1022 < 2 = NoReject connectionConnection rejected with 503 error
💡 Connections rejected when client IP reaches limit of 2 active connections.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
connections_192.168.1.100122122
connections_192.168.1.110000111
Key Moments - 2 Insights
Why does the connection get rejected at step 3 even though the client is the same?
At step 3, the current connections for 192.168.1.10 is already 2, which equals the limit. The condition 'connections < 2' is false, so nginx rejects the new connection as shown in the execution_table row 3.
What happens when one connection closes for a client IP?
When a connection closes, nginx decrements the count for that client IP. For example, after step 4, one connection from 192.168.1.10 closed, reducing its count from 2 to 1, allowing new connections again as seen in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the connection count for 192.168.1.10 at step 2?
A1
B0
C2
D3
💡 Hint
Check the 'Current Connections' column for step 2 in the execution_table.
At which step does nginx reject a connection from 192.168.1.10 due to limit reached?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Reject connection' action for 192.168.1.10 in the execution_table.
If the limit was increased to 3, what would happen at step 3?
AConnection would be rejected
BNo change, connection count resets
CConnection would be allowed
DServer would crash
💡 Hint
Refer to the condition 'connections < limit' in the execution_table and imagine limit=3.
Concept Snapshot
limit_conn_zone defines a shared memory zone keyed by client IP.
limit_conn sets max simultaneous connections per key.
Nginx checks current connections before accepting new ones.
If limit reached, new connections are rejected with error.
Counts decrement when connections close.
Useful to protect server from overload by single clients.
Full Transcript
This visual execution shows how nginx limits simultaneous connections per client IP using limit_conn. When a client connects, nginx checks how many active connections that IP has. If below the limit, nginx allows the connection and increments the count. If the limit is reached, nginx rejects the new connection with an error. When a connection closes, nginx decrements the count. The example limits each IP to 2 connections. Steps show connections accepted or rejected based on this limit, helping prevent overload from too many connections by one client.

Practice

(1/5)
1. What is the main purpose of the limit_conn directive in nginx?
easy
A. To limit the number of simultaneous connections per defined key
B. To limit the size of client requests
C. To limit the bandwidth used by clients
D. To limit the number of worker processes

Solution

  1. Step 1: Understand the role of limit_conn and compare options

    The limit_conn directive sets a maximum number of simultaneous connections for a given key, such as an IP address. Options A, B, and D relate to bandwidth, request size, and worker processes, which are not controlled by limit_conn.
  2. Final Answer:

    To limit the number of simultaneous connections per defined key -> Option A
  3. Quick Check:

    limit_conn limits connections = C [OK]
Hint: limit_conn controls max connections per key, not bandwidth or size [OK]
Common Mistakes:
  • Confusing limit_conn with bandwidth limiting
  • Thinking it limits request size
  • Assuming it controls worker processes
2. Which of the following is the correct syntax to limit connections per IP address using nginx directives?
easy
A. limit_conn_zone $remote_addr zone=addr:5m; limit_conn addr 10;
B. limit_conn_zone $remote_addr zone=addr:10m; limit_conn addr 5;
C. limit_conn_zone $binary_remote_addr zone=addr:10m; limit_conn addr 5;
D. limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 10;

Solution

  1. Step 1: Identify correct variable, zone size, and limit_conn usage

    The recommended variable for IP tracking is $binary_remote_addr and a zone size of 10m is common for storing states. The limit_conn directive uses the zone name (here 'addr') and the max connections (5) correctly in limit_conn_zone $binary_remote_addr zone=addr:10m; limit_conn addr 5;.
  2. Final Answer:

    limit_conn_zone $binary_remote_addr zone=addr:10m; limit_conn addr 5; -> Option C
  3. Quick Check:

    Correct variable and zone size = D [OK]
Hint: Use $binary_remote_addr and 10m zone for IP connection limits [OK]
Common Mistakes:
  • Using $remote_addr instead of $binary_remote_addr
  • Setting too small zone size like 5m
  • Mixing max connections and zone size values
3. Given this nginx configuration snippet:
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn addr 2;

What happens when a single IP tries to open 3 simultaneous connections?
medium
A. The third connection is rejected with a 503 error
B. All three connections are accepted without restriction
C. The third connection is queued until one connection closes
D. The server crashes due to too many connections

Solution

  1. Step 1: Understand limit_conn behavior and nginx response

    The directive limits simultaneous connections per key to 2, so the third connection exceeds the limit. When the limit is exceeded, nginx rejects the extra connection with HTTP status 503 (Service Unavailable).
  2. Final Answer:

    The third connection is rejected with a 503 error -> Option A
  3. Quick Check:

    Exceeding limit_conn returns 503 = B [OK]
Hint: Exceeding limit_conn max rejects connection with 503 error [OK]
Common Mistakes:
  • Thinking nginx queues extra connections
  • Assuming all connections are accepted
  • Believing server crashes on limit exceed
4. You wrote this nginx config:
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn addr 10m;

Why does nginx fail to start?
medium
A. Because zone size must be less than 5m
B. Because limit_conn_zone must be inside server block
C. Because $binary_remote_addr is invalid variable
D. Because the limit_conn directive expects a number, not '10m'

Solution

  1. Step 1: Check limit_conn directive argument and validate other parts

    The limit_conn directive expects a numeric value for max connections, not a size like '10m'. The limit_conn_zone syntax is correct and $binary_remote_addr is valid. Zone size 10m is allowed.
  2. Final Answer:

    Because the limit_conn directive expects a number, not '10m' -> Option D
  3. Quick Check:

    limit_conn needs number, not size string = A [OK]
Hint: limit_conn needs max connections number, not memory size [OK]
Common Mistakes:
  • Confusing zone size with max connections
  • Placing limit_conn_zone inside wrong block
  • Using invalid variables for IP tracking
5. You want to limit connections per user session instead of IP. Which configuration change achieves this using limit_conn directives?
hard
A. Use limit_conn_zone $remote_addr zone=sess:10m; and limit_conn sess 3;
B. Use limit_conn_zone $cookie_sessionid zone=sess:10m; and limit_conn sess 3;
C. Use limit_conn_zone $binary_remote_addr zone=sess:5m; and limit_conn sess 10;
D. Use limit_conn_zone $http_user_agent zone=sess:10m; and limit_conn sess 3;

Solution

  1. Step 1: Identify session key variable and configure directives

    To limit connections per user session, use a session identifier like a cookie variable, e.g., $cookie_sessionid. Define a zone with limit_conn_zone $cookie_sessionid zone=sess:10m; and limit connections with limit_conn sess 3;.
  2. Final Answer:

    Use limit_conn_zone $cookie_sessionid zone=sess:10m; and limit_conn sess 3; -> Option B
  3. Quick Check:

    Use cookie variable for session limits = A [OK]
Hint: Use cookie variable in limit_conn_zone for session-based limits [OK]
Common Mistakes:
  • Using IP variables instead of session cookie
  • Setting zone size too small for session keys
  • Using user agent which is not unique per session
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard