What is the difference between a location block and a server block in Nginx?
Learn the difference between Nginx server blocks and location blocks: how virtual hosts are matched by host and port, and how URIs route to location handlers.
Expected Interview Answer
A server block defines a virtual host — it decides which requests a server should answer based on the listening port and the requested host name — while a location block lives inside a server and decides how to handle requests whose URI path matches a given pattern.
Nginx first selects a server block by matching the connection's port (listen) and the Host header (server_name). Once a server is chosen, it evaluates that server's location blocks against the request URI to find the best match, and the winning location's directives determine the response — serving a file, proxying to a backend, or returning a redirect. In short, the server block answers 'which site?' and the location block answers 'which part of that site, and how?'
- Host many sites on one Nginx instance via separate server blocks
- Route different URL paths to different handlers with location blocks
- Apply per-path settings like caching or proxying precisely
- Keep virtual-host selection and URI routing as distinct concerns
- Support prefix, exact, and regex matching within locations
AI Mentor Explanation
A server block is like choosing which stadium a match is played in — decided by the venue and the fixture, much as Nginx picks a server by port and host name. A location block is like the specific instruction for each area inside that stadium: the pitch, the boundary, the pavilion each have their own rules. First you pick the ground, then you apply the rule for the exact spot the ball lands in.
Step-by-Step Explanation
Step 1
Match the server
Nginx uses the connection's listen port and the request's Host header to pick a server block; if none match, the default_server handles it.
Step 2
Read the request URI
Within the chosen server, Nginx takes the request path (e.g. /api/users) to route it.
Step 3
Evaluate location blocks
It tests the URI against the server's location patterns — exact (=), prefix, and regex (~) — to find the best match.
Step 4
Apply location directives
The winning location's directives decide the action: serve a file, proxy_pass, rewrite, or return.
Step 5
Fall back if needed
If no specific location matches, the prefix 'location /' acts as the catch-all handler.
What Interviewer Expects
- Server block selects the virtual host by port and server_name
- Location block routes by request URI within a server
- Understanding that locations are nested inside servers
- Knowledge of location match types (exact, prefix, regex)
- Awareness of default_server and the catch-all 'location /'
Common Mistakes
- Thinking a location block can exist outside a server block
- Confusing server_name matching with location URI matching
- Believing location match order is purely top-to-bottom (regex vs prefix precedence differs)
- Forgetting the catch-all 'location /' when other matches fail
- Mixing up 'listen' (server-level) with per-location directives
Best Answer (HR Friendly)
“A server block tells Nginx which website a request belongs to, based on the address and domain name. A location block, which sits inside a server block, tells Nginx what to do with specific parts of that website, like sending /images to one place and /api to another.”
Code Example
http {
server { # virtual host: which site?
listen 80;
server_name example.com;
location = /health { # exact match
return 200 'ok';
}
location /static/ { # prefix match
root /var/www;
}
location /api/ { # prefix match — proxy to backend
proxy_pass http://127.0.0.1:3000;
}
location / { # catch-all fallback
try_files $uri $uri/ =404;
}
}
}Follow-up Questions
- How does Nginx decide which location wins when multiple patterns match?
- What is the difference between prefix, exact (=), and regex (~) location matches?
- What role does server_name play in choosing a server block?
- What is a default_server and when is it used?
- Can location blocks be nested inside one another?
MCQ Practice
1. Which block determines which virtual host answers a request?
The server block is matched by listen port and server_name to select the virtual host; location blocks route within it.
2. A location block is always defined?
Location blocks are nested inside a server block and route requests by URI after the server has been selected.
3. Which location handles requests when no more specific pattern matches?
The prefix 'location /' matches every URI and acts as the catch-all when nothing more specific matches.
Flash Cards
What does a server block do? — Defines a virtual host, matched by listen port and server_name to decide which site answers a request.
What does a location block do? — Routes requests by URI within a server, deciding how a matching path is handled.
Where do location blocks live? — Always nested inside a server block; they cannot exist on their own.
What is the catch-all location? — 'location /' matches any URI and handles requests when no more specific location matches.
Continue Learning
Related Interview Questions
How does the Nginx configuration file structure work with contexts and directives?
medium
What is the difference between proxy_pass and fastcgi_pass in Nginx?
medium
What is a reverse proxy and how does Nginx act as one?
medium
What is the difference between the Nginx master process and worker processes?
medium