What is the purpose of the 'default_server' directive in nginx?
The 'default_server' directive marks a server block as the default to handle requests that do not match any other server_name. It ensures nginx knows which server block to use when no specific match is found.
Click to reveal answer
beginner
How do you specify a default server for HTTP traffic in nginx?
You add 'default_server' to the listen directive, for example: listen 80 default_server; This tells nginx to use this server block as the default for port 80.
Click to reveal answer
intermediate
What happens if no default_server is set in nginx and a request does not match any server_name?
Nginx will use the first server block defined for that listen port as the default. This can lead to unexpected behavior if you don't explicitly set a default server.
Click to reveal answer
intermediate
Can you have multiple default servers for the same port in nginx?
No, only one server block per listen port can be marked as 'default_server'. Trying to set multiple default servers for the same port will cause nginx to fail to start.
Click to reveal answer
intermediate
How does nginx decide which server block to use when a request comes in?
Nginx first matches the request's IP and port to a listen directive. Then it matches the Host header to server_name. If no match, it uses the default_server for that listen port.
Click to reveal answer
Which directive marks a server block as the default in nginx?
Aserver_default
Bdefault_server
Cdefault_block
Dserver_main
✗ Incorrect
The 'default_server' directive is used in the listen directive to mark a server block as the default.
What happens if no 'default_server' is set and a request does not match any server_name?
ANginx returns an error
BNginx ignores the request
CNginx uses the first server block for that port
DNginx redirects to a default page
✗ Incorrect
Nginx uses the first server block defined for the listen port as the default if no 'default_server' is set.
How do you set a server block as default for HTTPS on port 443?
Alisten 443;
Bserver_name default 443;
Cdefault_server 443;
Dlisten 443 default_server;
✗ Incorrect
You add 'default_server' to the listen directive like 'listen 443 default_server;' to set it as default for HTTPS.
Can you have two server blocks with 'default_server' on the same port?
ANo, nginx will fail to start
BYes, nginx will load balance
CYes, but only one will respond
DNo, but nginx will ignore one
✗ Incorrect
Only one server block per port can be 'default_server'. Multiple defaults cause nginx startup failure.
What does nginx use to match incoming requests to server blocks?
AIP, port, and server_name
BPort only
CIP address only
Dserver_name only
✗ Incorrect
Nginx matches requests by IP and port (listen directive) and then by server_name.
Explain how nginx decides which server block handles a request when multiple server blocks exist.
Think about the order nginx uses to find the right server block.
You got /3 concepts.
Describe how to configure a default server in nginx and why it is important.
Focus on the 'listen' directive and request routing.
You got /3 concepts.
Practice
(1/5)
1. What does the default_server directive do in an nginx configuration?
easy
A. It sets the server block to only respond to HTTPS requests.
B. It disables the server block from responding to any requests.
C. It redirects all requests to another server block.
D. It marks the server block to respond when no other server name matches the request.
Solution
Step 1: Understand the role of default_server
The default_server directive in the listen line tells nginx which server block to use if no other server name matches the request.
Step 2: Analyze the options
It marks the server block to respond when no other server name matches the request. correctly describes this behavior. Options A, B, and D describe unrelated or incorrect behaviors.
Final Answer:
It marks the server block to respond when no other server name matches the request. -> Option D
Quick Check:
default_server = marks fallback server [OK]
Hint: default_server means fallback server for unmatched requests [OK]
Common Mistakes:
Thinking default_server disables the server
Confusing default_server with HTTPS enforcement
Assuming default_server redirects requests
2. Which of the following is the correct syntax to set a server block as the default server for HTTP on port 80?
easy
A. listen default_server 80;
B. listen 80 default_server;
C. default_server listen 80;
D. listen 80 default;
Solution
Step 1: Recall nginx listen directive syntax
The correct syntax places the port number first, then the default_server flag: listen 80 default_server;.
Step 2: Evaluate each option
listen 80 default_server; matches the correct syntax. Options A and C have incorrect order. listen 80 default; uses an invalid keyword default.
Final Answer:
listen 80 default_server; -> Option B
Quick Check:
listen port default_server order matters [OK]
Hint: Put port number before default_server in listen line [OK]
Common Mistakes:
Swapping order of port and default_server
Using invalid keywords like default instead of default_server
Placing default_server outside listen directive
3. Given this nginx config snippet, what response will nginx serve for a request to IP address 192.168.1.10?
Believing nginx merges server blocks automatically
5. You want nginx to serve a custom page for all requests that do not match any server_name, including requests by IP or unknown hostnames. How should you configure the default server block to achieve this?
hard
A. Use listen 80 default_server; and set server_name localhost;.
B. Use listen 80; without default_server and set server_name _;.
C. Use listen 80 default_server; and set server_name _; with a custom return or root directive.
D. Use listen 80 default_server; and omit server_name entirely.
Solution
Step 1: Mark the server block as default_server
To catch unmatched requests, the server block must have listen 80 default_server;.
Step 2: Use server_name _; as a wildcard
Setting server_name _; is a common convention to match any hostname not matched elsewhere.
Step 3: Configure custom response
Inside this block, use return or root to serve the custom page.
Final Answer:
Use listen 80 default_server; and set server_name _; with a custom return or root directive. -> Option C
Quick Check:
default_server + server_name _ = catch-all server [OK]
Hint: default_server plus server_name _ catches all unmatched requests [OK]
Common Mistakes:
Not using default_server flag
Omitting server_name or using localhost only
Assuming default_server alone matches all hostnames
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.