Rate Limiting with nginx using limit_req_zone and limit_req
📖 Scenario: You are managing a web server that receives many requests. To keep the server stable and fair for all users, you want to limit how many requests each user can make per second.This project will guide you to configure nginx to limit request rates using limit_req_zone and limit_req.
🎯 Goal: Build an nginx configuration that limits each user's request rate to 1 request per second with a small burst allowance.
📋 What You'll Learn
Create a shared memory zone named one with a 10MB size using limit_req_zone
Use the client IP address as the key for rate limiting
Set the rate limit to 1 request per second
Apply the rate limit in the server block using limit_req
Allow a burst of 2 requests
💡 Why This Matters
🌍 Real World
Web servers often face many requests from users. Limiting request rates prevents overload and abuse, ensuring fair access and server stability.
💼 Career
Understanding nginx rate limiting is important for DevOps roles managing web infrastructure and ensuring reliable service availability.
Progress0 / 4 steps
1
Create the limit_req_zone directive
Write a limit_req_zone directive that creates a shared memory zone called one with size 10m. Use $binary_remote_addr as the key and set the rate to 1r/s.
Nginx
Hint
Use limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; to create the zone.
2
Add the server block with listen directive
Add a server block that listens on port 80.
Nginx
Hint
Use server { listen 80; } to start the server block.
3
Apply limit_req inside the server block
Inside the server block, add a limit_req directive that uses the zone one and allows a burst of 2.
Nginx
Hint
Use limit_req zone=one burst=2; inside the server block.
4
Print the final nginx configuration
Print the complete nginx configuration to verify your setup.
Nginx
Hint
Use print() to display the full configuration as text.
Practice
(1/5)
1. What is the main purpose of the limit_req_zone directive in nginx?
easy
A. To log requests that exceed the limit
B. To apply the rate limit rules to a specific location
C. To define the key and rate limit parameters for request limiting
D. To restart nginx when limits are reached
Solution
Step 1: Understand the role of limit_req_zone
This directive sets the shared memory zone and key used to track requests for limiting.
Step 2: Differentiate from limit_req
limit_req applies the rules, but limit_req_zone defines them.
Final Answer:
To define the key and rate limit parameters for request limiting -> Option C
Quick Check:
limit_req_zone defines limits [OK]
Hint: Remember: zone defines limits, limit_req applies them [OK]
Common Mistakes:
Confusing limit_req_zone with limit_req
Thinking it applies limits directly
Assuming it logs requests
2. Which of the following is the correct syntax to define a rate limit zone named mylimit with 10MB memory and 5 requests per second per IP?
easy
A. limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;
B. limit_req_zone zone=mylimit:10m $binary_remote_addr rate=5r/s;
C. limit_req_zone $remote_addr zone=mylimit:5m rate=10r/s;
D. limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/m;
Solution
Step 1: Check the correct order of parameters
The syntax is limit_req_zone key zone=name:size rate=rate; so key first, then zone with name and size, then rate.
Step 2: Validate values
$binary_remote_addr is preferred key, zone name is mylimit, size is 10m, rate is 5r/s (requests per second).
Final Answer:
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s; -> Option A
But nginx logs show no rate limiting is applied. What is the likely error?
medium
A. Incorrect key used in limit_req_zone
B. The limit_req_zone directive must be inside server block
C. No nodelay option causing silent ignoring
D. Missing limit_req directive inside server block
Solution
Step 1: Check key correctness
Using an incorrect key in limit_req_zone (e.g., $remote_addr instead of the preferred $binary_remote_addr) prevents proper rate limiting from being applied.
Step 2: Confirm directive placement
limit_req_zone must be in http context, limit_req in server/location; placement here is correct.
Final Answer:
Missing limit_req directive inside server block -> Option D
Quick Check:
limit_req directive is required to apply limits [OK]
Hint: Check that limit_req directive is present inside server/location block [OK]
Common Mistakes:
Placing limit_req_zone inside server block
Assuming nodelay is required
Not matching key with client IP
5. You want to limit requests to 10 per second per user IP but allow bursts of up to 20 requests without delay. Which configuration correctly achieves this?
hard
A. limit_req_zone $binary_remote_addr zone=fastlimit:10m rate=10r/s;
limit_req zone=fastlimit burst=20 nodelay;
B. limit_req_zone $remote_addr zone=fastlimit:10m rate=20r/s;
limit_req zone=fastlimit burst=10;
C. limit_req_zone $binary_remote_addr zone=fastlimit:10m rate=10r/s;
limit_req zone=fastlimit burst=20;
D. limit_req_zone $remote_addr zone=fastlimit:20m rate=10r/s;
limit_req zone=fastlimit burst=10 nodelay;
Solution
Step 1: Define correct rate and burst
Rate is 10r/s, burst 20 means allow 20 extra requests queued without delay.
Step 2: Use nodelay to avoid delaying burst requests
nodelay makes burst requests processed immediately, matching requirement.
Step 3: Validate key and zone size
$binary_remote_addr is preferred key, 10m memory is sufficient.