Bird
Raised Fist0
Nginxdevops~5 mins

server_name directive in Nginx - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you run a website on a server, you need to tell the server which website to show when someone types a web address. The server_name directive in nginx helps the server know which website to serve based on the web address the visitor uses.
When you host multiple websites on the same server and need to direct visitors to the right site.
When you want to respond to different domain names or subdomains with different content.
When you want to set a default website for requests that don't match any specific domain.
When you want to handle www and non-www versions of your website separately.
When you want to use wildcard domains to catch many subdomains with one configuration.
Config File - nginx.conf
nginx.conf
server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80;
    server_name blog.example.com;

    root /var/www/blog;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

This configuration defines two server blocks. Each block listens on port 80 (standard web traffic).

The server_name directive tells nginx which domain names this block will respond to.

The first block responds to example.com and www.example.com, serving files from /var/www/example.

The second block responds to blog.example.com, serving files from /var/www/blog.

Commands
This command tests the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command sends a request to example.com to check if nginx serves the correct website.
Terminal
curl -I http://example.com
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive
This command sends a request to blog.example.com to verify nginx serves the blog site correctly.
Terminal
curl -I http://blog.example.com
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:05 GMT Content-Type: text/html Content-Length: 450 Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: the server_name directive tells nginx which website to serve based on the visitor's web address.

Common Mistakes
Forgetting to reload nginx after changing the server_name directive.
Nginx will continue using the old configuration, so changes won't take effect.
Always run 'sudo nginx -t' to test and then 'sudo systemctl reload nginx' to apply changes.
Using incorrect domain names or typos in the server_name directive.
Nginx won't match requests correctly, causing visitors to see the wrong site or an error.
Double-check domain names for accuracy and test with curl or a browser.
Not including both www and non-www versions if both should work.
Visitors using one version may not reach the intended site.
List all needed domain variants in server_name or use redirects.
Summary
The server_name directive tells nginx which domain names a server block should respond to.
After editing server_name, always test the configuration with 'nginx -t' and reload nginx.
Use curl or a browser to verify that nginx serves the correct website for each domain.

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