Bird
Raised Fist0
Nginxdevops~20 mins

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

Choose your learning style10 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.

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

  1. 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.
  2. Step 2: Identify what location blocks control

    They control routing of requests to different handlers or backends based on the URL.
  3. Final Answer:

    Routes incoming requests based on URL paths -> Option D
  4. 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

  1. Step 1: Recall nginx location modifiers

    The = modifier means exact match of the URL path.
  2. Step 2: Match the exact path syntax

    Using location = /api/v1/users matches only that exact path.
  3. Final Answer:

    location = /api/v1/users { ... } -> Option A
  4. Quick Check:

    Exact match uses = modifier [OK]
Hint: Use '=' for exact path match in location [OK]
Common Mistakes:
  • Using no modifier for exact match
  • Using regex (~) when exact match needed
  • Using ^~ which is prefix but not exact
3. Given this nginx config snippet:
location /api/ {
  proxy_pass http://backend-service/;
}

What happens when a client requests /api/users?
medium
A. Request is forwarded to http://backend-service/users
B. Request is forwarded to http://backend-service/api/users
C. Request is served locally from /api/users directory
D. Request returns 404 Not Found

Solution

  1. Step 1: Understand proxy_pass with trailing slash

    When proxy_pass URL ends with a slash, nginx replaces the matching part of the request URI.
  2. Step 2: Apply to the request path

    The matching prefix /api/ is replaced by / from proxy_pass, so /api/users becomes /users at backend.
  3. Final Answer:

    Request is forwarded to http://backend-service/users -> Option A
  4. Quick Check:

    proxy_pass with trailing slash replaces prefix [OK]
Hint: Trailing slash in proxy_pass replaces matched prefix [OK]
Common Mistakes:
  • Assuming proxy_pass appends full original URI
  • Confusing local serving with proxying
  • Ignoring trailing slash effect in proxy_pass
4. Identify the error in this nginx location block:
location /api {
  proxy_pass http://backend-service/;
}
medium
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

  1. 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.
  2. Step 2: Correct usage for subpath matching

    To match /api and all subpaths, use location /api/ with trailing slash.
  3. Final Answer:

    location path should end with a slash to match subpaths -> Option B
  4. 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?
hard
A. location /api/v2/status { return 200 '{"status":"ok"}'; } location ^~ /api/v2/ { proxy_pass http://v2-backend/; }
B. location /api/v2/status { proxy_pass http://v2-backend/status; } location /api/v2/ { proxy_pass http://v2-backend/; }
C. location = /api/v2/status { return 200 '{"status":"ok"}'; } location /api/v2/ { proxy_pass http://v2-backend/; }
D. location /api/v2/ { proxy_pass http://v2-backend/; } location = /api/v2/status { proxy_pass http://v2-backend/status; }

Solution

  1. Step 1: Use exact match for static response

    Use location = /api/v2/status to match exactly that path and return static JSON.
  2. Step 2: Use prefix match for all other /api/v2/ requests

    Use location /api/v2/ to proxy all other requests to backend.
  3. Final Answer:

    location = /api/v2/status { return 200 '{"status":"ok"}'; } location /api/v2/ { proxy_pass http://v2-backend/; } -> Option C
  4. Quick Check:

    Exact match first, then prefix proxy [OK]
Hint: Exact match location before prefix for special cases [OK]
Common Mistakes:
  • Placing prefix location before exact match causing override
  • Using proxy_pass for static JSON response
  • Not escaping JSON properly in return directive
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard