0
0
Nginxdevops~10 mins

server_name directive in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - server_name directive
Start: HTTP request arrives
Check Host header
Match server_name directive?
NoDefault server handles request
Yes
Route request to matching server block
Process request with matched server config
Send response back to client
The server_name directive matches the Host header of incoming requests to route them to the correct server block.
Execution Sample
Nginx
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example;
}
This config routes requests with Host 'example.com' or 'www.example.com' to this server block.
Process Table
StepIncoming Host headerserver_name match?ActionResult
1example.comYesRoute to this server blockServe content from /var/www/example
2www.example.comYesRoute to this server blockServe content from /var/www/example
3test.comNoCheck other server blocks or defaultRequest handled by default server
4example.netNoCheck other server blocks or defaultRequest handled by default server
💡 Request is routed to the server block whose server_name matches the Host header; otherwise default server handles it.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Host headernoneexample.comwww.example.comtest.comexample.net
Matched server blocknoneexample.com blockexample.com blocknonenone
Key Moments - 2 Insights
Why does a request with Host 'test.com' not match the server_name?
Because 'test.com' is not listed in the server_name directive (see execution_table row 3), so nginx routes it to the default server.
Can server_name match multiple names?
Yes, server_name can list multiple names separated by spaces (see execution_sample), so requests with any of those names match this server block.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which Host header matches the server_name directive?
Aexample.com
Btest.com
Cexample.net
Dnone of the above
💡 Hint
Check the 'server_name match?' column in execution_table rows 1-4.
At which step does nginx route the request to the default server?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for 'No' in 'server_name match?' and 'Request handled by default server' in execution_table.
If we add 'test.com' to server_name, what changes in the execution table?
AStep 1 would no longer match
BStep 3 would show a match and route to this server block
CStep 4 would match instead of default
DNo changes would occur
💡 Hint
Adding 'test.com' to server_name means requests with Host 'test.com' match this block (see execution_table row 3).
Concept Snapshot
server_name directive:
- Defines domain names nginx listens for
- Matches Host header in HTTP requests
- Multiple names separated by spaces
- If no match, default server handles request
- Used to route requests to correct server block
Full Transcript
The server_name directive in nginx tells the server which domain names it should respond to. When a request arrives, nginx looks at the Host header and compares it to the names listed in server_name. If it finds a match, it sends the request to that server block. If not, the default server handles the request. You can list multiple names in server_name separated by spaces to match different domains. This helps nginx know which website content to serve based on the domain the user typed.