Bird
Raised Fist0
Nginxdevops~20 mins

Connection limiting (limit_conn) in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Connection Limiting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the effect of this nginx configuration snippet?
Given the following nginx configuration, what will be the output or effect when more than 5 simultaneous connections are made from the same IP address?
Nginx
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
    location / {
        limit_conn addr 5;
        proxy_pass http://backend;
    }
}
ARequests exceeding 5 simultaneous connections from the same IP will be delayed but eventually processed.
BRequests exceeding 5 simultaneous connections from the same IP will be rejected with a 503 Service Unavailable error.
CRequests exceeding 5 simultaneous connections from the same IP will be queued and processed sequentially.
DRequests exceeding 5 simultaneous connections from the same IP will be redirected to a different server.
Attempts:
2 left
💡 Hint
Think about what limit_conn does when the connection limit is reached.
❓ Configuration
intermediate
2:00remaining
Choose the correct configuration to limit connections per user session
You want to limit simultaneous connections per user session identified by a cookie named 'SESSIONID' to 3. Which configuration snippet correctly sets this up?
A
limit_conn_zone $remote_addr zone=session:10m;
limit_conn session 3;
B
limit_conn_zone $binary_remote_addr zone=session:10m;
limit_conn session 3;
C
limit_conn_zone $http_cookie zone=session:10m;
limit_conn session 3;
D
limit_conn_zone $cookie_SESSIONID zone=session:10m;
limit_conn session 3;
Attempts:
2 left
💡 Hint
Use the variable that extracts the cookie value.
❓ Troubleshoot
advanced
2:00remaining
Why does the limit_conn directive not seem to work in this config?
You configured nginx with: limit_conn_zone $binary_remote_addr zone=addr:10m; server { limit_conn addr 10; location / { proxy_pass http://backend; } } But connections are not limited as expected. What is the most likely cause?
AThe limit_conn_zone directive must be inside the server block, not the http block.
BThe proxy_pass directive disables limit_conn functionality.
CThe limit_conn directive must be inside the location block, not the server block.
DThe zone size 10m is too small to track connections.
Attempts:
2 left
💡 Hint
Check where limit_conn is allowed to be used.
🔀 Workflow
advanced
2:00remaining
Order the steps to enable connection limiting per IP in nginx
Put these steps in the correct order to enable connection limiting per IP address in nginx.
A1,2,3,4
B2,1,3,4
C2,3,1,4
D1,3,2,4
Attempts:
2 left
💡 Hint
Think about configuration, applying, then testing.
✅ Best Practice
expert
2:00remaining
Which approach best prevents denial-of-service attacks using connection limiting?
You want to protect your nginx server from denial-of-service attacks by limiting connections. Which configuration approach is best practice?
AUse limit_conn_zone with $binary_remote_addr and limit_conn in location blocks, combined with limit_req to limit request rate.
BUse limit_conn_zone with $remote_addr and set limit_conn to 1 globally without request rate limiting.
CUse limit_conn_zone with $http_user_agent and limit_conn to block suspicious user agents only.
DUse limit_conn_zone with $binary_remote_addr and disable keepalive connections to reduce load.
Attempts:
2 left
💡 Hint
Think about combining connection and request rate limits for better protection.

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