Bird
Raised Fist0
Nginxdevops~10 mins

server_name directive in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - server_name directive
Start: HTTP request arrives
↓
Check Host header
↓
Match server_name directive?
No→Default 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.

Practice

(1/5)
1. What is the main purpose of the server_name directive in an nginx configuration?
easy
A. To define the port number for the server
B. To specify which domain names the server should respond to
C. To set the root directory for website files
D. To configure the SSL certificate path

Solution

  1. Step 1: Understand the role of server_name

    The server_name directive tells nginx which domain names it should respond to when a request arrives.
  2. Step 2: Compare with other directives

    Other directives like listen set ports, root sets file paths, and SSL directives configure certificates, not domain names.
  3. Final Answer:

    To specify which domain names the server should respond to -> Option B
  4. Quick Check:

    server_name = domain names [OK]
Hint: server_name always sets domains nginx listens for [OK]
Common Mistakes:
  • Confusing server_name with listen port
  • Thinking server_name sets file paths
  • Mixing server_name with SSL settings
2. Which of the following is the correct syntax to set multiple domain names in the server_name directive?
easy
A. server_name example.com www.example.com;
B. server_name = example.com, www.example.com;
C. server_name: example.com www.example.com;
D. server_name example.com; www.example.com;

Solution

  1. Step 1: Recall nginx syntax for server_name

    The correct syntax lists domain names separated by spaces, ending with a semicolon.
  2. Step 2: Analyze each option

    server_name example.com www.example.com; correctly lists domains separated by spaces and ends with a semicolon. Options B and C use invalid characters (=, :, commas). server_name example.com; www.example.com; incorrectly splits domains into two statements.
  3. Final Answer:

    server_name example.com www.example.com; -> Option A
  4. Quick Check:

    Multiple domains separated by spaces [OK]
Hint: List domains with spaces, end with semicolon [OK]
Common Mistakes:
  • Using commas between domain names
  • Adding equal signs or colons
  • Splitting domains into multiple directives
3. Given this nginx server block snippet:
server {
    listen 80;
    server_name *.example.com example.org;
}

Which domain will nginx respond to?
medium
A. Only example.org
B. Only subdomains of example.com
C. Both example.org and any subdomain of example.com
D. Any domain ending with .com

Solution

  1. Step 1: Understand the wildcard in server_name

    The *.example.com matches any subdomain like www.example.com or blog.example.com, but not example.com itself.
  2. Step 2: Check the other domain

    The directive also includes example.org, so nginx responds to that exact domain as well.
  3. Final Answer:

    Both example.org and any subdomain of example.com -> Option C
  4. Quick Check:

    wildcard + exact domain = both matched [OK]
Hint: Wildcard matches subdomains; list exact domains separately [OK]
Common Mistakes:
  • Thinking wildcard matches main domain without subdomain
  • Assuming wildcard matches all .com domains
  • Ignoring exact domain entries
4. You wrote this nginx config:
server {
    listen 80;
    server_name www.example.com, example.org;
}

Why does nginx fail to start?
medium
A. Because semicolon is missing after server_name
B. Because the listen directive is missing a port
C. Because server_name must be inside location block
D. Because commas are not allowed between domain names

Solution

  1. Step 1: Identify syntax error in server_name

    The server_name directive expects domain names separated by spaces, not commas.
  2. Step 2: Check other parts of config

    The listen directive correctly specifies port 80. server_name is valid in server block, and semicolon is present.
  3. Final Answer:

    Because commas are not allowed between domain names -> Option D
  4. Quick Check:

    No commas allowed in server_name list [OK]
Hint: Separate domains with spaces, never commas [OK]
Common Mistakes:
  • Using commas between domains
  • Placing server_name inside location block
  • Omitting semicolon at end
5. You want nginx to respond to example.com, www.example.com, and any subdomain like blog.example.com. Which server_name directive is correct?
hard
A. server_name example.com www.example.com *.example.com;
B. server_name example.com www.example.com;
C. server_name *.example.com;
D. server_name www.example.com example.com*;

Solution

  1. Step 1: Understand domain matching with wildcards

    The wildcard *.example.com matches subdomains like blog.example.com but does NOT match the root domain example.com.
  2. Step 2: Include all required domains explicitly

    To cover example.com and www.example.com, they must be listed explicitly along with the wildcard.
  3. Final Answer:

    server_name example.com www.example.com *.example.com; -> Option A
  4. Quick Check:

    Wildcard excludes root domain, list all needed domains [OK]
Hint: Wildcard excludes root domain; list it explicitly [OK]
Common Mistakes:
  • Assuming wildcard covers root domain
  • Using invalid wildcard syntax like example.com*
  • Omitting www or root domain
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