📖 Scenario: You are managing a busy website that needs to handle different types of requests efficiently. Some requests need special treatment, like redirecting users based on their location or blocking bad traffic. Simple rules are not enough, so you want to use advanced nginx patterns to solve these complex needs.
🎯 Goal: Build an nginx configuration that uses advanced patterns like regular expressions and conditional blocks to route requests differently based on URL paths and user IP addresses.
📋 What You'll Learn
Create a server block listening on port 80
Use a variable $blocked_ips containing a list of IPs to block
Use map directive to set a variable $is_blocked based on $remote_addr
Use location blocks with regex to match URLs starting with /api/ and /admin/
Use if condition to return 403 Forbidden for blocked IPs
Use return directives to redirect or serve content based on conditions
💡 Why This Matters
🌍 Real World
Web servers often need to handle many types of requests and block bad traffic. Using advanced nginx patterns helps manage complex routing and security rules efficiently.
💼 Career
Understanding advanced nginx configuration is important for DevOps engineers and system administrators to secure and optimize web services.
Progress0 / 4 steps
1
Create the initial server block
Create an nginx server block that listens on port 80 and has a root directory set to /var/www/html.
Nginx
Hint
Use server {} block with listen 80; and root /var/www/html; inside.
2
Add blocked IPs list and map variable
Add a variable $blocked_ips with the value "192.168.1.10 10.0.0.5". Then use the map directive to create a variable $is_blocked that is 1 if $remote_addr matches any IP in $blocked_ips, otherwise 0.
Nginx
Hint
Use map $remote_addr $is_blocked { ... } outside the server block to set $is_blocked.
3
Add location blocks with regex and blocking logic
Inside the server block, add two location blocks: one matching URLs starting with /api/ using regex ~ ^/api/, and another matching URLs starting with /admin/ using regex ~ ^/admin/. Inside the server block, add an if condition that returns 403 Forbidden if $is_blocked equals 1.
Nginx
Hint
Use if ($is_blocked = 1) { return 403; } inside server. Use regex location ~ ^/api/ {} and location ~ ^/admin/ {}.
4
Add redirects and print final configuration
Inside the /admin/ location block, add a return 302 directive to redirect requests to /login. Then add a location / block that returns a simple HTML page with text Welcome to the homepage. Finally, print the entire nginx configuration file content.
Nginx
Hint
Use return 302 /login; inside /admin/ location. Add location / { return 200 '...'; } for homepage. Print full config.
Practice
(1/5)
1. What is the main benefit of using advanced nginx patterns in server configuration?
easy
A. They allow handling complex routing and conditional responses.
B. They reduce the server's memory usage significantly.
C. They automatically update nginx to the latest version.
D. They simplify the installation process of nginx.
Solution
Step 1: Understand nginx pattern purpose
Advanced patterns in nginx help manage complex routing and conditions for requests.
Step 2: Compare options
Only They allow handling complex routing and conditional responses. correctly states the benefit of handling complex routing and conditional responses.
Final Answer:
They allow handling complex routing and conditional responses. -> Option A
Quick Check:
Advanced patterns = complex routing [OK]
Hint: Advanced patterns handle complex routes and conditions [OK]
Common Mistakes:
Confusing performance improvements with routing benefits
Thinking nginx auto-updates with patterns
Assuming patterns simplify installation
2. Which of the following is a correct nginx directive syntax to match requests only for the domain example.com?
easy
A. server_name = example.com;
B. server_name example.com;
C. server_name ~ example.com;
D. server_name !example.com;
Solution
Step 1: Recall server_name directive syntax
The correct syntax to specify a domain is server_name example.com; without extra symbols.
Step 2: Analyze other options
Options B, C, and D use invalid or unsupported syntax for server_name.
What happens when a POST request is sent to /api/data?
medium
A. The server returns HTTP status 405 Method Not Allowed.
B. The request is proxied to http://backend.
C. The request is redirected to /api/ with GET method.
D. The server returns HTTP status 404 Not Found.
Solution
Step 1: Analyze the if condition
The config checks if the request method is POST and returns 405 if true.
Step 2: Apply condition to POST request
Since the request is POST, nginx returns 405 and does not proxy the request.
Final Answer:
The server returns HTTP status 405 Method Not Allowed. -> Option A
Quick Check:
POST request triggers 405 return [OK]
Hint: If condition returns 405 on POST, request won't proxy [OK]
Common Mistakes:
Assuming proxy_pass always runs regardless of if
Confusing 405 with 404 status codes
Thinking request is redirected instead of blocked
4. You wrote this nginx config to redirect HTTP to HTTPS:
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
But the redirect does not work. What is the likely error?
medium
A. The server block must listen on port 443 for redirects.
B. Missing semicolon after listen 80 directive.
C. Using $server_name instead of $host in the return statement.
D. Incorrect HTTP status code; should be 302 instead of 301.
Solution
Step 1: Understand variable usage in redirect
$server_name may not hold the requested host; $host is preferred for redirects.
Step 2: Check other options
Semicolon is present, 301 is correct for permanent redirect, and listening on 80 is correct for HTTP to HTTPS redirect.
Final Answer:
Using $server_name instead of $host in the return statement. -> Option C
Quick Check:
Use $host for redirects, not $server_name [OK]
Hint: Use $host, not $server_name, for redirect URLs [OK]
Common Mistakes:
Confusing $server_name with $host variable
Thinking 301 is wrong status code for redirect
Believing listen 443 is needed for HTTP to HTTPS redirect
5. You want nginx to serve static files from /var/www/static only if the request URI starts with /static/, otherwise proxy to backend. Which config snippet correctly implements this advanced pattern?