0
0
Nginxdevops~10 mins

API routing with location blocks in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - API routing with location blocks
Client sends HTTP request
Nginx receives request
Check location blocks in order
Match found?
NoReturn 404 or default
Yes
Route request to matched location
Serve response from backend or static files
Nginx receives a request, checks location blocks in order, routes to the matching block, or returns default if none match.
Execution Sample
Nginx
server {
  listen 80;
  location /api/ {
    proxy_pass http://backend_api;
  }
  location / {
    root /var/www/html;
  }
}
Routes requests starting with /api/ to backend_api, others serve static files from /var/www/html.
Process Table
StepRequest URILocation Block CheckedMatch ResultAction Taken
1/api/userslocation /api/MatchProxy to backend_api
2/api/userslocation /Not checked (first match found)No action
3/homelocation /api/No matchCheck next location
4/homelocation /MatchServe static files from /var/www/html
5/unknownlocation /api/No matchCheck next location
6/unknownlocation /MatchServe static files from /var/www/html
7/notfoundlocation /api/No matchCheck next location
8/notfoundlocation /MatchServe static files from /var/www/html
💡 Request routed to first matching location block or default if none match.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
Request URI/api/users/home/home/home
Matched LocationNone/api/None//
ActionNoneProxy to backend_apiNoneServe static filesServe static files
Key Moments - 3 Insights
Why does Nginx stop checking location blocks after the first match?
Nginx uses the first matching location block to route the request, so once a match is found (see Step 1), it stops checking others to improve performance.
What happens if no location block matches the request URI?
If no location matches, Nginx uses the default server behavior, often returning a 404 error or serving the root location if defined (not shown in this example).
Why does /home match the location / block and not /api/?
Because /home does not start with /api/, so the /api/ location block does not match (Step 3), but / matches all URIs, so it matches at Step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what action is taken for the request URI '/api/users' at Step 1?
AServe static files
BReturn 404 error
CProxy to backend_api
DCheck next location block
💡 Hint
Check the 'Action Taken' column at Step 1 in the execution_table.
At which step does the request URI '/home' match the location block '/'?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look at the 'Match Result' column for '/home' in the execution_table.
If a new location block 'location /api/v2/' is added before 'location /api/', which request URI would match it first?
A/api/v2/items
B/home
C/api/users
D/api/
💡 Hint
Consider prefix matching order and the execution flow of location blocks.
Concept Snapshot
Nginx API routing uses location blocks to match request URIs.
The first matching location block handles the request.
Use prefix matching like 'location /api/' for API routes.
Order matters: more specific blocks should come first.
Unmatched requests go to default or return 404.
Full Transcript
When a client sends an HTTP request, Nginx receives it and checks its location blocks in order. It compares the request URI to each location block's prefix. Once it finds a match, it routes the request accordingly, such as proxying to a backend API or serving static files. If no location matches, Nginx returns a default response or error. For example, requests starting with '/api/' go to the backend API, while others serve static content. This process stops at the first match to keep routing efficient.