What is the purpose of the server_name directive in nginx?
The server_name directive tells nginx which domain names or IP addresses a server block should respond to. It helps nginx decide which website to serve based on the requested URL.
Click to reveal answer
beginner
How do you specify multiple domain names in the server_name directive?
You list them separated by spaces, like server_name example.com www.example.com;. Nginx will respond to any of these names in the same server block.
Click to reveal answer
intermediate
What happens if no server_name matches the requested domain?
Nginx uses the default server block, usually the first one defined, to respond. This is like a fallback when no specific domain matches.
Click to reveal answer
intermediate
Can server_name use wildcards? Give an example.
Yes, wildcards can be used. For example, server_name *.example.com; matches any subdomain like blog.example.com or shop.example.com.
Click to reveal answer
advanced
What is the difference between server_name _; and server_name *;?
server_name _; is often used as a catch-all default server name. The underscore is a convention and not a wildcard. server_name *; is invalid syntax in nginx.
Click to reveal answer
What does the server_name directive specify in nginx?
AThe IP address of the server
BThe domain names the server block responds to
CThe port number to listen on
DThe root directory of the website
✗ Incorrect
The server_name directive tells nginx which domain names to match for this server block.
How would you configure nginx to respond to both example.com and www.example.com?
Multiple names are separated by spaces, not commas.
Which of these is a valid wildcard usage in server_name?
Aserver_name *.example.com;
Bserver_name example.*;
Cserver_name example.com*;
Dserver_name *example.com;
✗ Incorrect
Wildcards can only be used at the start or end, like *.example.com.
What happens if no server_name matches the request?
ANginx redirects to google.com
BNginx returns a 404 error
CNginx uses the default server block
DNginx shuts down
✗ Incorrect
Nginx falls back to the default server block if no names match.
Is server_name *; a valid nginx configuration?
ANo, it is invalid syntax
BYes, but only for IP addresses
CYes, it matches all domains
DYes, but only for localhost
✗ Incorrect
server_name *; is invalid. Use server_name _; for a catch-all name.
Explain how the server_name directive works in nginx and why it is important.
Think about how nginx knows which website to show when many domains point to the same server.
You got /5 concepts.
Describe how to configure nginx to serve multiple subdomains using the server_name directive.
Consider how to cover main domain and all subdomains in one server block.
You got /3 concepts.
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
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.
Step 2: Compare with other directives
Other directives like listen set ports, root sets file paths, and SSL directives configure certificates, not domain names.
Final Answer:
To specify which domain names the server should respond to -> Option B
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
Step 1: Recall nginx syntax for server_name
The correct syntax lists domain names separated by spaces, ending with a semicolon.
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.
Final Answer:
server_name example.com www.example.com; -> Option A
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
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.
Step 2: Check the other domain
The directive also includes example.org, so nginx responds to that exact domain as well.
Final Answer:
Both example.org and any subdomain of example.com -> Option C
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
Step 1: Identify syntax error in server_name
The server_name directive expects domain names separated by spaces, not commas.
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.
Final Answer:
Because commas are not allowed between domain names -> Option D
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
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.
Step 2: Include all required domains explicitly
To cover example.com and www.example.com, they must be listed explicitly along with the wildcard.
Final Answer:
server_name example.com www.example.com *.example.com; -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.