0
0
Nginxdevops~20 mins

API routing with location blocks in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Routing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the response for a request to /api/v1/users?
Given the following nginx configuration, what will be the response status code when a client requests /api/v1/users?
Nginx
server {
  listen 80;
  location /api/ {
    return 200 "API root";
  }
  location /api/v1/ {
    return 404;
  }
}
A502 Bad Gateway
B200 with body 'API root'
C404 Not Found
D301 Moved Permanently
Attempts:
2 left
💡 Hint
Nginx chooses the most specific matching location block.
Configuration
intermediate
2:00remaining
Which location block correctly proxies /api/v2/ requests?
You want to proxy all requests starting with /api/v2/ to http://backend:8080. Which location block configuration achieves this correctly?
A
location /api/v2/ {
  proxy_pass http://backend:8080;
}
B
location /api/v2/ {
  proxy_pass http://backend:8080/;
}
C
location /api/v2 {
  proxy_pass http://backend:8080/;
}
D
location /api/v2 {
  proxy_pass http://backend:8080;
}
Attempts:
2 left
💡 Hint
Trailing slash in proxy_pass affects URI rewriting.
Troubleshoot
advanced
2:30remaining
Why does /api/v1/data return 404 despite correct backend?
Given this nginx config, requests to /api/v1/data return 404. What is the likely cause?
Nginx
server {
  listen 80;
  location /api/ {
    proxy_pass http://backend:8080/api/;
  }
  location /api/v1/ {
    proxy_pass http://backend:8080/v1/;
  }
}
Aproxy_pass URIs cause double /api/v1/ in backend request, leading to 404
BThe proxy_pass in /api/v1/ should not have a trailing slash
CThe /api/v1/ location is overridden by /api/ because it appears first
DNginx does not support nested location blocks
Attempts:
2 left
💡 Hint
Check how proxy_pass rewrites the URI with trailing slashes.
🔀 Workflow
advanced
3:00remaining
Order the steps to add a new API version routing in nginx
Arrange the steps to add routing for /api/v3/ that proxies to http://backend:9090/v3/.
A1,3,2,4
B1,3,4,2
C3,1,4,2
D1,4,3,2
Attempts:
2 left
💡 Hint
You must reload nginx before testing changes.
Best Practice
expert
3:00remaining
Which configuration best avoids overlapping location conflicts?
You have multiple API versions: /api/v1/, /api/v2/, and a generic /api/. Which nginx config best avoids conflicts and ensures correct routing?
A
location /api/ {
  proxy_pass http://backend/api/;
}
location /api/v1/ {
  proxy_pass http://backend/v1/;
}
location /api/v2/ {
  proxy_pass http://backend/v2/;
}
B
location /api/v1/ {
  proxy_pass http://backend/v1/;
}
location /api/v2/ {
  proxy_pass http://backend/v2/;
}
location /api/ {
  proxy_pass http://backend/api/;
  rewrite ^/api/(.*)$ /api/v1/$1 break;
}
C
location /api/v1/ {
  proxy_pass http://backend/v1/;
}
location /api/v2/ {
  proxy_pass http://backend/v2/;
}
location = /api/ {
  proxy_pass http://backend/api/;
}
D
location ^~ /api/v1/ {
  proxy_pass http://backend/v1/;
}
location ^~ /api/v2/ {
  proxy_pass http://backend/v2/;
}
location /api/ {
  proxy_pass http://backend/api/;
}
Attempts:
2 left
💡 Hint
Use ^~ to prioritize prefix matches over regex or generic locations.