0
0
Nginxdevops~5 mins

server_name directive in Nginx - Commands & Configuration

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