0
0
Nginxdevops~5 mins

Why virtual hosting serves multiple domains in Nginx - Why It Works

Choose your learning style9 modes available
Introduction
When you want to run several websites on one server, virtual hosting lets you do that by directing visitors to the right site based on the website address they use. This saves money and resources by sharing one server for many domains.
When you want to host multiple websites on a single server without buying separate servers for each.
When you want to save costs by sharing server resources among different websites.
When you want to manage different websites with different domain names on the same IP address.
When you want to test multiple websites locally on one machine before going live.
When you want to organize websites for different clients on one server but keep them separate.
Config File - nginx.conf
nginx.conf
events {}
http {
    server {
        listen 80;
        server_name example.com;
        root /var/www/example.com;
        index index.html;
    }

    server {
        listen 80;
        server_name testsite.com;
        root /var/www/testsite.com;
        index index.html;
    }
}

This configuration file tells Nginx to listen on port 80 for two different domain names: example.com and testsite.com.

Each server block defines settings for one domain, including where its website files live (root) and the default page (index).

Nginx uses the server_name to decide which website to show when a visitor comes in.

Commands
This command checks if the Nginx configuration file is correct before restarting the server to avoid errors.
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, so websites stay online.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command simulates a visitor requesting example.com to verify Nginx serves the correct website based on the domain name.
Terminal
curl -H "Host: example.com" http://localhost
Expected OutputExpected
<html>...content of example.com homepage...</html>
This command simulates a visitor requesting testsite.com to verify Nginx serves the correct website for that domain.
Terminal
curl -H "Host: testsite.com" http://localhost
Expected OutputExpected
<html>...content of testsite.com homepage...</html>
Key Concept

Virtual hosting lets one server use the website address visitors type to decide which site to show, so multiple domains share one server smoothly.

Common Mistakes
Forgetting to set the correct server_name for each server block.
Nginx won't know which website to serve, so visitors may see the wrong site or an error.
Always specify the exact domain name in the server_name directive for each website.
Not testing the configuration with 'nginx -t' before reloading.
Errors in the config can cause Nginx to fail to reload, making websites unavailable.
Run 'sudo nginx -t' to check for syntax errors before reloading.
Using the same root directory for multiple domains.
Different websites will show the same content, losing separation between sites.
Use separate root directories for each domain to keep their files separate.
Summary
Virtual hosting uses the server_name to serve different websites from one server.
Each website has its own server block with its own root folder and domain name.
Always test Nginx configuration before reloading to avoid downtime.