How to Configure Nginx Server Blocks for Subdomains Sharing the Same IP Address


7 views

When multiple domains or subdomains point to the same IP address, Nginx uses the server_name directive to determine which server block should handle the incoming request. The key configuration element is proper DNS setup combined with correct Nginx server block definitions.

# Main domain configuration
server {
    listen 80;
    server_name blabla.com www.blabla.com;
    root /var/www/blabla;
    index index.html;

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

# Subdomain configuration
server {
    listen 80;
    server_name mew.blabla.com;
    root /var/www/mew;
    index index.html;

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

1. Verify DNS records using dig mew.blabla.com or nslookup mew.blabla.com
2. Ensure both domains resolve to the same IP (103.35.123.4 in your case)
3. Check Nginx syntax with sudo nginx -t
4. Reload Nginx after changes: sudo systemctl reload nginx

Problem: Subdomain requests fall to default server block
Fix: Ensure default_server isn't set on your main domain's listen directive

Problem: DNS propagation delays
Fix: Verify with direct IP access using Host header:

curl -H "Host: mew.blabla.com" http://103.35.123.4

For dynamic subdomains, use regex pattern matching:

server {
    listen 80;
    server_name ~^(?.+)\.blabla\.com$;
    root /var/www/$subdomain;
    ...
}

1. Create test files in both root directories
2. Use curl with explicit Host headers
3. Check Nginx access logs for request routing
4. Verify SSL certificates if using HTTPS


When multiple domains or subdomains point to the same IP address, Nginx uses the server_name directive to determine which server block should handle each request. The common mistake is not properly setting up the DNS records or missing crucial Nginx configuration elements.

First, verify your DNS records. Both your main domain and subdomain should have proper A records:

blabla.com.    IN  A  103.35.123.4.12
mew.blabla.com. IN  A  103.35.123.4.12

Here's the correct Nginx configuration:

# Main domain configuration
server {
    listen 80;
    server_name blabla.com www.blabla.com;
    
    root /var/www/primary;
    index index.html;
    
    location / {
        try_files $uri $uri/ =404;
    }
}

# Subdomain configuration
server {
    listen 80;
    server_name mew.blabla.com;
    
    root /var/www/subdomain;
    index index.html;
    
    location / {
        try_files $uri $uri/ =404;
    }
}

1. DNS Propagation Delay: After setting up DNS records, wait for propagation (usually 5-30 minutes)

2. Default Server Block: Ensure you don't have a catch-all server block intercepting requests

3. Nginx Reload: After configuration changes, run:

sudo nginx -t && sudo systemctl reload nginx

Use these commands to verify:

# Check DNS resolution
dig +short mew.blabla.com

# Test Nginx configuration
curl -I http://mew.blabla.com
curl -I http://blabla.com

For dynamic subdomains, use a wildcard configuration:

server {
    listen 80;
    server_name ~^(?.+)\.blabla\.com$;
    
    root /var/www/$subdomain;
    
    location / {
        try_files $uri $uri/ =404;
    }
}