0
0
Nginxdevops~10 mins

Default server handling in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Default server handling
Client sends HTTP request
Nginx receives request
Check server_name in all server blocks
Response sent
Nginx receives a request, tries to match the server_name in server blocks, and if none match, uses the default server block to respond.
Execution Sample
Nginx
server {
    listen 80 default_server;
    server_name _;
    return 444;
}

server {
    listen 80;
    server_name example.com;
    return 200;
}
Two server blocks: one default server that closes connections, one for example.com that returns 200 OK.
Process Table
StepClient Request HostServer Block MatchedAction TakenResponse
1example.comserver_name example.comProcess example.com server block200 OK
2unknown.comNo matchUse default_server blockConnection closed (444)
💡 Requests with no matching server_name use the default_server block to handle the request.
Status Tracker
VariableStartRequest 1Request 2Final
server_name_matchednullexample.comnullnull
used_server_blocknullexample.com blockdefault_server blockdefault_server block
response_codenull200444444
Key Moments - 2 Insights
Why does nginx use the default server block when the requested host does not match any server_name?
Because nginx needs a fallback to handle requests without a matching server_name, it uses the server block marked with 'default_server' as shown in execution_table row 2.
What happens if multiple server blocks listen on the same port but none is marked as default_server?
Nginx will use the first server block defined for that port as the default, similar to the default_server block behavior, ensuring requests are handled.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response does nginx send when the client requests 'example.com'?
A200 OK
B444 Connection Closed
C404 Not Found
D500 Internal Server Error
💡 Hint
Check execution_table row 1 under 'Response' column.
At which step does nginx use the default server block?
AStep 1
BNever
CStep 2
DBoth steps
💡 Hint
Look at execution_table 'Server Block Matched' column for 'No match' entry.
If the default_server directive is removed, what will nginx do with unmatched requests?
AReturn 404 Not Found
BUse the first server block listening on the port
CClose connection with 444
DIgnore the request
💡 Hint
Refer to key_moments second question about default_server behavior.
Concept Snapshot
Nginx default server handling:
- Nginx matches client Host header to server_name.
- If no match, uses server block with 'default_server' on listen directive.
- Default server block handles unmatched requests.
- Without default_server, first server block on port is default.
- Useful for fallback or rejecting unknown hosts.
Full Transcript
When nginx receives a request, it looks at the Host header to find a matching server_name in its server blocks. If it finds a match, it uses that server block to process the request and send a response. If no server_name matches, nginx uses the server block marked with 'default_server' as a fallback. This ensures every request is handled. If no default_server is set, nginx uses the first server block listening on that port as the default. This behavior helps nginx decide how to respond to requests with unknown or missing hostnames.