📖 Scenario: You are managing a web server that hosts an API. The API has two versions: v1 and v2. You want to route incoming requests to the correct API version based on the URL path.For example, requests to /api/v1/ should go to the v1 backend, and requests to /api/v2/ should go to the v2 backend.
🎯 Goal: Configure Nginx to route API requests to the correct backend based on the API version in the URL path.
📋 What You'll Learn
Create an Nginx server block listening on port 80
Route requests starting with /api/v1/ to the backend at http://localhost:5001
Route requests starting with /api/v2/ to the backend at http://localhost:5002
Return a 404 error for any other API paths
💡 Why This Matters
🌍 Real World
Many companies maintain multiple versions of their APIs to support old and new clients. Routing requests correctly ensures smooth operation and backward compatibility.
💼 Career
Understanding how to configure Nginx for API versioning is a common task for DevOps engineers and backend developers managing web services.
Progress0 / 4 steps
1
Create the basic Nginx server block
Create an Nginx server block that listens on port 80 and has a root location / that returns a simple text response "Welcome to API server".
Nginx
Hint
Use listen 80; inside the server block. Use location / { return 200 'Welcome to API server'; } to respond with text.
2
Add routing for API version 1
Inside the existing server block, add a location block for /api/v1/ that proxies requests to http://localhost:5001.
Nginx
Hint
Use location /api/v1/ { proxy_pass http://localhost:5001; } inside the server block.
3
Add routing for API version 2
Inside the existing server block, add a location block for /api/v2/ that proxies requests to http://localhost:5002.
Nginx
Hint
Use location /api/v2/ { proxy_pass http://localhost:5002; } inside the server block.
4
Return 404 for other API paths
Inside the existing server block, add a location block for /api/ that returns a 404 error for any paths not matched by previous locations.
Nginx
Hint
Use location /api/ { return 404; } to catch other API paths.
Practice
(1/5)
1. What is the main purpose of using API versioning with routing in nginx?
easy
A. To block all API requests except the latest version
B. To combine all API versions into a single endpoint
C. To run multiple API versions side by side and route requests accordingly
D. To disable routing and serve static files only
Solution
Step 1: Understand API versioning concept
API versioning allows different versions of an API to coexist, so clients using older versions still work.
Step 2: Role of nginx routing
nginx uses location blocks to route requests to the correct API version based on the URL path.
Final Answer:
To run multiple API versions side by side and route requests accordingly -> Option C
Quick Check:
API versioning = multiple versions side by side [OK]
Hint: API versioning means routing requests to different versions [OK]
Common Mistakes:
Thinking API versioning disables old versions
Assuming all versions merge into one endpoint
Confusing routing with blocking requests
2. Which nginx configuration snippet correctly routes requests to API version v1 at path /api/v1/?
easy
A. location /api/v1/ { proxy_pass http://backend_v1; }
B. location /api/v1 { proxy_pass http://backend_v1; }
C. location /api/v1/* { proxy_pass http://backend_v1; }
D. location /api/v1$ { proxy_pass http://backend_v1; }
Solution
Step 1: Understand nginx location matching
Using location /api/v1/ matches all requests starting with /api/v1/, including subpaths.
Step 2: Check syntax correctness
location /api/v1/ { proxy_pass http://backend_v1; } uses correct syntax without wildcards or regex, which nginx does not support in this form.
Final Answer:
location /api/v1/ { proxy_pass http://backend_v1; } -> Option A
Quick Check:
Trailing slash in location matches subpaths [OK]
Hint: Use trailing slash in location to match all subpaths [OK]
Common Mistakes:
Using wildcards (*) in location which nginx does not support
Missing trailing slash causing partial matches
Using regex syntax without ~
3. Given this nginx config snippet, what backend will handle a request to /api/v2/users?
D. Missing trailing slash in first location causes incorrect routing
Solution
Step 1: Check location path consistency
The first location uses /api/v1 without trailing slash, which matches only exact path or prefix differently than /api/v1/.
Step 2: Understand routing impact
Requests like /api/v1/users won't match the first location correctly, causing routing errors.
Final Answer:
Missing trailing slash in first location causes incorrect routing -> Option D
Quick Check:
Trailing slash in location matters for prefix matching [OK]
Hint: Always use trailing slash in location for API version prefixes [OK]
Common Mistakes:
Ignoring trailing slash difference in location paths
Thinking proxy_pass URL must have trailing slash
Believing location blocks cannot be next to each other
5. You want to support API versions v1 and v2 in nginx, but also redirect any request to /api/ without version to v2 by default. Which config snippet achieves this correctly?