How to Configure Nginx Server Blocks for Multiple Domains
In this tutorial, you'll learn about How to Configure Nginx Server Blocks for Multiple Domains. We cover key concepts, practical examples, and best practices.
The Problem
You point two domains (example.com and example.org) to the same server IP, but Nginx serves the same site for both. This happens when there is only one server block in the configuration, or when the server_name directive is missing or set incorrectly. Nginx uses the first matching server block and treats any request without an explicit match as the default server.
Quick Fix
1. Create a separate server block for each domain
For example.com — /etc/nginx/sites-available/example.com:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
For example.org — /etc/nginx/sites-available/example.org:
server {
listen 80;
listen [::]:80;
server_name example.org www.example.org;
root /var/www/example.org/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
2. Enable both sites
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/example.org /etc/nginx/sites-enabled/
3. Create the document roots and add content
sudo mkdir -p /var/www/example.com/html
sudo mkdir -p /var/www/example.org/html
echo '<h1>Welcome to Example.com</h1>' | sudo tee /var/www/example.com/html/index.html
echo '<h1>Welcome to Example.org</h1>' | sudo tee /var/www/example.org/html/index.html
4. Test the configuration
sudo nginx -t
Expected output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5. Reload Nginx
sudo systemctl reload nginx
Expected output:
# (no output — config reloaded)
6. Verify with curl using the Host header
curl -H "Host: example.com" http://localhost
curl -H "Host: example.org" http://localhost
Expected output:
<h1>Welcome to Example.com</h1>
<h1>Welcome to Example.org</h1>
7. Set up a catch-all default server
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/default;
return 404;
}
Additional Troubleshooting
# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"
If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.
8. Check for duplicate server_name conflicts
nginx -T 2>&1 | grep -A2 "server_name"
If the same server_name appears in multiple blocks, only the first one matches. Rename or remove the duplicate.
Prevention
- Use descriptive file names in
sites-availablethat match the domain name - Always run
nginx -tbefore reloading to catch syntax errors - Add a
default_serverblock to handle requests for unknown domain names - Keep document roots outside system directories to simplify permission management
- Test with
curl -H "Host:"before updating DNS to verify the config works correctly
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro