0
0
Nginxdevops~20 mins

Default server handling in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Server Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What happens when a request matches no server_name?
Given this nginx configuration snippet:
server {
    listen 80 default_server;
    server_name _;
    return 200 'Default server response';
}

server {
    listen 80;
    server_name example.com;
    return 200 'Example.com response';
}

What will be the response body when a request is sent to IP address directly (no Host header or unknown Host)?
Nginx
server {
    listen 80 default_server;
    server_name _;
    return 200 'Default server response';
}

server {
    listen 80;
    server_name example.com;
    return 200 'Example.com response';
}
AThe response body will be 'Default server response'
BThe response body will be 'Example.com response'
CThe request will be rejected with 404 Not Found
DThe request will timeout with no response
Attempts:
2 left
💡 Hint
Think about which server block nginx uses when no server_name matches.
Configuration
intermediate
2:00remaining
Identify the correct default server configuration
Which nginx server block configuration correctly sets a default server for HTTP port 80?
A
server {
    listen 80 default_server;
    server_name _;
    return 200 'Default';
}
B
server {
    listen 80;
    server_name default_server;
    return 200 'Default';
}
C
server {
    listen 80 default;
    server_name _;
    return 200 'Default';
}
D
server {
    listen 80 default_server on;
    server_name _;
    return 200 'Default';
}
Attempts:
2 left
💡 Hint
The 'default_server' keyword must be used with 'listen'.
Troubleshoot
advanced
2:00remaining
Why does nginx serve the wrong server block?
You have two server blocks:
server {
    listen 80 default_server;
    server_name example.com;
    return 200 'Example.com default';
}

server {
    listen 80;
    server_name test.com;
    return 200 'Test.com';
}

You send a request with Host header 'test.com' but nginx responds with 'Example.com default'. What is the likely cause?
AThe first server block is default_server and matches all requests, ignoring server_name
BThe second server block has a syntax error and is ignored
CNginx does not support multiple server blocks on the same port
DThe Host header is missing in the request, so default_server is used
Attempts:
2 left
💡 Hint
Check if the Host header is correctly sent in the request.
🔀 Workflow
advanced
2:00remaining
Order the steps to set a default server in nginx
Put these steps in the correct order to configure a default server for HTTP requests in nginx:
A1,3,2,4
B2,1,3,4
C1,2,3,4
D1,2,4,3
Attempts:
2 left
💡 Hint
Think about editing config, then reloading, then testing.
Best Practice
expert
2:00remaining
Which is the best practice for default server handling in nginx?
Choose the best practice for configuring a default server in nginx to handle unmatched requests safely and clearly.
AUse multiple server blocks with the same server_name and rely on nginx to load balance between them
BUse a dedicated server block with 'listen 80 default_server;' and a generic server_name like '_' that returns a simple 444 or 404 response
CSet the first server block without default_server and rely on nginx to pick the first block as default
DDo not configure a default server; let nginx return 502 Bad Gateway for unmatched requests
Attempts:
2 left
💡 Hint
Think about clarity and security for unmatched requests.