📖 Scenario: You are setting up a simple web server using nginx. Your goal is to route different API requests to different backend services using location blocks.This is like sorting mail into different boxes based on the address on the envelope.
🎯 Goal: Build an nginx configuration that routes requests starting with /api/users to one backend and requests starting with /api/products to another backend.
📋 What You'll Learn
Create an nginx server block with a root path
Add a location block for /api/users routing to http://users_backend
Add a location block for /api/products routing to http://products_backend
Add a default location block for all other requests routing to http://default_backend
💡 Why This Matters
🌍 Real World
Routing API requests to different backend services is common in microservices and web applications.
💼 Career
Understanding nginx location blocks is essential for DevOps roles managing web servers and load balancing.
Progress0 / 4 steps
1
Create the basic server block
Create an nginx server block listening on port 80 with a root path /var/www/html. Use server {} and listen 80; inside it.
Nginx
Hint
Use server {} to start the block, then add listen 80; and root /var/www/html; inside.
2
Add location block for /api/users
Inside the existing server block, add a location block for /api/users that proxies requests to http://users_backend using proxy_pass.
Nginx
Hint
Use location /api/users {} and inside it add proxy_pass http://users_backend;.
3
Add location block for /api/products
Inside the server block, add another location block for /api/products that proxies requests to http://products_backend using proxy_pass.
Nginx
Hint
Add location /api/products {} with proxy_pass http://products_backend; inside.
4
Add default location block and print config
Add a default location block / inside the server block that proxies requests to http://default_backend. Then print the full nginx configuration file content.
Nginx
Hint
Add location / { proxy_pass http://default_backend; } inside the server block.
Practice
(1/5)
1. What does a location block in an nginx configuration do?
easy
A. Sets the maximum file upload size
B. Defines server IP addresses
C. Specifies the server's operating system
D. Routes incoming requests based on URL paths
Solution
Step 1: Understand the purpose of location blocks
Location blocks in nginx are used to match parts of the URL path to decide how to handle requests.
Step 2: Identify what location blocks control
They control routing of requests to different handlers or backends based on the URL.
Final Answer:
Routes incoming requests based on URL paths -> Option D
Quick Check:
Location blocks = URL routing [OK]
Hint: Location blocks match URL paths to route requests [OK]
Common Mistakes:
Confusing location blocks with server IP settings
Thinking location blocks set server OS
Assuming location blocks control upload size
2. Which of the following is the correct syntax to define a location block that matches exactly the path /api/v1/users?
easy
A. location = /api/v1/users { ... }
B. location /api/v1/users { ... }
C. location ~ /api/v1/users { ... }
D. location ^~ /api/v1/users { ... }
Solution
Step 1: Recall nginx location modifiers
The = modifier means exact match of the URL path.
Step 2: Match the exact path syntax
Using location = /api/v1/users matches only that exact path.
Final Answer:
location = /api/v1/users { ... } -> Option A
Quick Check:
Exact match uses = modifier [OK]
Hint: Use '=' for exact path match in location [OK]
A. Missing trailing slash in location path causes wrong proxying
B. location path should end with a slash to match subpaths
C. proxy_pass URL must not have a trailing slash
D. No error; configuration is correct
Solution
Step 1: Check location path matching behavior
Without a trailing slash, location /api matches only the exact path /api and not subpaths like /api/users.
Step 2: Correct usage for subpath matching
To match /api and all subpaths, use location /api/ with trailing slash.
Final Answer:
location path should end with a slash to match subpaths -> Option B
Quick Check:
Trailing slash in location matches subpaths [OK]
Hint: Add trailing slash in location to match subpaths [OK]
Common Mistakes:
Thinking proxy_pass trailing slash is wrong here
Assuming location without slash matches subpaths
Believing config is error-free without checking path
5. You want to route all requests starting with /api/v2/ to http://v2-backend/ but requests exactly to /api/v2/status should return a static JSON response locally. Which nginx config achieves this?