Bird
Raised Fist0
Nginxdevops~20 mins

Default server handling in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It marks the server block to respond when no other server name matches the request. -> Option D
  4. 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

  1. Step 1: Recall nginx listen directive syntax

    The correct syntax places the port number first, then the default_server flag: listen 80 default_server;.
  2. 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.
  3. Final Answer:

    listen 80 default_server; -> Option B
  4. 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?
server {
    listen 80 default_server;
    server_name default.example.com;
    return 200 'Default Server';
}

server {
    listen 80;
    server_name site.example.com;
    return 200 'Site Server';
}
medium
A. Returns 'Default Server'
B. Returns 'Site Server'
C. Returns 404 Not Found
D. Returns 500 Internal Server Error

Solution

  1. Step 1: Identify which server block matches the IP request

    The request is to IP 192.168.1.10, which does not match any server_name directive.
  2. Step 2: Determine which server handles unmatched requests

    The server block with listen 80 default_server; handles requests without matching server_name, so it responds.
  3. Final Answer:

    Returns 'Default Server' -> Option A
  4. Quick Check:

    IP requests hit default_server block [OK]
Hint: Requests without matching server_name go to default_server [OK]
Common Mistakes:
  • Assuming IP requests match any server_name
  • Thinking unmatched requests return 404 automatically
  • Confusing default_server with error handling
4. You configured two server blocks both with listen 80 default_server;. What will happen when nginx starts?
medium
A. nginx will fail to start due to conflicting default_server directives.
B. nginx will pick the first default_server block and ignore the second.
C. nginx will load both and round-robin requests between them.
D. nginx will merge both server blocks into one.

Solution

  1. Step 1: Understand default_server uniqueness

    Only one server block per IP:port can be marked as default_server. Multiple default_server flags on same port cause conflict.
  2. Step 2: Check nginx behavior on conflict

    nginx detects this conflict and refuses to start, showing an error about duplicate default_server.
  3. Final Answer:

    nginx will fail to start due to conflicting default_server directives. -> Option A
  4. Quick Check:

    Only one default_server per port allowed [OK]
Hint: Only one default_server per port; duplicates cause startup error [OK]
Common Mistakes:
  • Thinking nginx picks one silently
  • Assuming nginx load balances default_server blocks
  • 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

  1. Step 1: Mark the server block as default_server

    To catch unmatched requests, the server block must have listen 80 default_server;.
  2. Step 2: Use server_name _; as a wildcard

    Setting server_name _; is a common convention to match any hostname not matched elsewhere.
  3. Step 3: Configure custom response

    Inside this block, use return or root to serve the custom page.
  4. Final Answer:

    Use listen 80 default_server; and set server_name _; with a custom return or root directive. -> Option C
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard