0
0
Nginxdevops~20 mins

Why location matching controls request routing in Nginx - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NGINX Location Routing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does NGINX decide which location block handles a request?

NGINX uses location blocks to route requests. Which statement best explains how NGINX chooses the correct location block?

ANGINX always uses the first location block defined in the configuration regardless of the request URI.
BNGINX randomly picks any location block that contains the requested URI substring.
CNGINX matches the request URI against location blocks in order, using the first match it finds.
DNGINX selects the location block with the longest matching prefix or the best regex match for the request URI.
Attempts:
2 left
💡 Hint

Think about how NGINX tries to find the most specific match for a request URI.

💻 Command Output
intermediate
2:00remaining
Which backend handles /app/v3/page?

Given this NGINX config snippet:

server {
  listen 80;
  location /app/ {
    proxy_pass http://backend1;
  }
  location /app/v2/ {
    proxy_pass http://backend2;
  }
}

What happens if a request is made to /app/v3/page?

AThe request is routed to backend1 because /app/ is the longest matching prefix.
BThe request is routed to backend2 because /app/v2/ is the closest match.
CNGINX returns a 404 error because no location matches /app/v3/page exactly.
DNGINX returns a 500 error due to configuration conflict.
Attempts:
2 left
💡 Hint

Consider prefix matching rules and which location block matches the request URI.

Configuration
advanced
2:00remaining
Which location block will handle the request?

Consider this NGINX configuration:

location /images/ {
  root /data;
}
location ~* \.(gif|jpg|jpeg)$ {
  root /images;
}

What location block handles a request for /images/cat.jpg?

ABoth location blocks handle the request simultaneously.
BThe second location block because the regex matches the file extension .jpg.
CThe first location block because it matches the prefix /images/.
DNGINX returns a 404 error because of conflicting location blocks.
Attempts:
2 left
💡 Hint

Remember that regex locations have higher priority than prefix locations if they match.

Troubleshoot
advanced
2:00remaining
Why does the backend receive the full URI?

An NGINX server has these location blocks:

location /api/ {
  proxy_pass http://api_backend;
}
location /api/v1/ {
  proxy_pass http://api_v1_backend;
}

A request to /api/v1/users is proxied to http://api_v1_backend/api/v1/users instead of http://api_v1_backend/users. Why?

ABecause the proxy_pass directive is missing a trailing slash in /api/v1/ location.
BBecause the /api/ location is defined before /api/v1/, NGINX matches it first and stops.
CBecause NGINX does not support nested location blocks and ignores /api/v1/.
DBecause the /api/v1/ location is a prefix but /api/ is an exact match.
Attempts:
2 left
💡 Hint

Check how proxy_pass handles URI rewriting with and without trailing slashes.

Best Practice
expert
3:00remaining
What is the best way to ensure correct routing with overlapping location blocks?

You have multiple overlapping location blocks in NGINX. What is the best practice to control routing precisely?

APlace the most general location block first and rely on NGINX to pick the best match automatically.
BDefine all location blocks as prefix matches without regex to avoid confusion.
CUse exact match (=) for the most specific URIs and regex (~ or ~*) for pattern matching, placing exact matches first.
DUse only regex locations to handle all routing cases for consistency.
Attempts:
2 left
💡 Hint

Think about how NGINX prioritizes exact, prefix, and regex locations.