0
0
Nginxdevops~20 mins

API versioning with routing in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Versioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of NGINX routing for API version v1
Given the following NGINX configuration snippet, what will be the response status code when a client requests /api/v1/users?
Nginx
server {
    listen 80;
    location /api/v1/ {
        proxy_pass http://backend_v1/;
    }
    location /api/v2/ {
        proxy_pass http://backend_v2/;
    }
}
A200 OK from backend_v1
B404 Not Found
C502 Bad Gateway
D301 Moved Permanently
Attempts:
2 left
💡 Hint
Check which location block matches the requested URI.
Configuration
intermediate
2:00remaining
Correct NGINX config for API versioning with header
Which NGINX configuration snippet correctly routes requests to different backends based on the custom header X-API-Version with values v1 or v2?
A
server {
    listen 80;
    location /api/ {
        proxy_pass http://backend_v1/;
        if ($http_x_api_version = 'v2') {
            proxy_pass http://backend_v2/;
        }
    }
}
B
map $http_x_api_version $backend {
    default backend_v1;
    v2 backend_v2;
}
server {
    listen 80;
    location /api/ {
        proxy_pass http://$backend/;
    }
}
C
server {
    listen 80;
    if ($http_x_api_version = 'v2') {
        proxy_pass http://backend_v2/;
    }
    location /api/ {
        proxy_pass http://backend_v1/;
    }
}
D
server {
    listen 80;
    location /api/ {
        proxy_pass http://backend_v1/;
    }
    location /api/v2/ {
        proxy_pass http://backend_v2/;
    }
}
Attempts:
2 left
💡 Hint
Use the map directive to select backend based on header.
Troubleshoot
advanced
2:00remaining
Troubleshoot NGINX API version routing failure
A user reports that requests to /api/v2/orders always return 404 Not Found, but /api/v1/orders works fine. Given this NGINX config, what is the most likely cause?
Nginx
server {
    listen 80;
    location /api/v1/ {
        proxy_pass http://backend_v1/;
    }
    location /api/v2 {
        proxy_pass http://backend_v2/;
    }
}
Aproxy_pass URL missing trailing slash in /api/v1 location
Bbackend_v2 is down causing 404 errors
CMissing trailing slash in location /api/v2 causes URI mismatch
DNGINX does not support multiple location blocks with similar prefixes
Attempts:
2 left
💡 Hint
Check how NGINX matches location blocks and rewrites URIs with proxy_pass.
🔀 Workflow
advanced
2:00remaining
Order of NGINX location blocks for API versioning
Given these location blocks, what is the correct order to ensure requests to /api/v2/users route to the v2 backend and /api/users route to the default backend?
A1,3,2
B2,3,1
C3,1,2
D1,2,3
Attempts:
2 left
💡 Hint
More specific location blocks must come before less specific ones.
Best Practice
expert
3:00remaining
Best practice for API versioning in NGINX with minimal config duplication
Which NGINX configuration approach best minimizes duplication while supporting multiple API versions with shared common settings?
AUse regex locations for each version with repeated proxy settings
BCreate separate location blocks for each version with duplicated proxy settings
CUse <code>if</code> statements inside location to switch proxy_pass URLs
DUse a single location /api/ with <code>map</code> to select backend based on header and common proxy settings inside location
Attempts:
2 left
💡 Hint
Think about how to reuse proxy settings and avoid repeating code.