Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Nginx Subdomains Different Document Roots

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to configure nginx so that each subdomain (like blog.example.com or shop.example.com) serves files from its own directory on the server. Instead of cramming every site into one folder, you get clean separation for easier maintenance, independent deployments, and clearer access logs. You'll need root or sudo access to your server, a working nginx installation, and DNS records pointing each subdomain to your server's IP address.

What You'll Need

  • A server running Linux (Ubuntu, Debian, CentOS, etc.) with nginx installed.
  • Root or sudo access to edit files in /etc/nginx/.
  • DNS A records for each subdomain pointing to your server's public IP (e.g., blog.example.com → 203.0.113.10).
  • Separate directories for each subdomain's files, e.g., /var/www/blog, /var/www/shop.
  • A text editor like nano or vim.

Step-by-Step

  1. Create the document root directories. For each subdomain, create a folder where its files will live. For example:
    sudo mkdir -p /var/www/blog
    sudo mkdir -p /var/www/shop
    Set the ownership so your web user can read them (usually www-data on Debian/Ubuntu, nginx on RHEL/CentOS):
    sudo chown -R $USER:www-data /var/www/blog
    sudo chown -R $USER:www-data /var/www/shop
  2. Create a test file in each directory. This helps verify that each subdomain is serving the correct root later. For instance:
    echo "Blog subdomain" | sudo tee /var/www/blog/index.html
    echo "Shop subdomain" | sudo tee /var/www/shop/index.html
  3. Create a server block for each subdomain. Nginx uses server blocks (similar to Apache virtual hosts). The cleanest way is to create a separate file per subdomain in /etc/nginx/sites-available/ (Debian/Ubuntu) or /etc/nginx/conf.d/ (RHEL/CentOS). For this example, we'll use the Debian layout.
    sudo nano /etc/nginx/sites-available/blog.example.com
    Paste the following, adjusting the server_name, root, and any SSL lines to match your setup:
    server {
        listen 80;
        listen [::]:80;
        server_name blog.example.com;
        root /var/www/blog;
        index index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
    Save and exit. Repeat for the shop subdomain, creating /etc/nginx/sites-available/shop.example.com with root /var/www/shop;.
  4. Enable the server blocks. On Debian/Ubuntu, create symlinks in sites-enabled:
    sudo ln -s /etc/nginx/sites-available/blog.example.com /etc/nginx/sites-enabled/
    sudo ln -s /etc/nginx/sites-available/shop.example.com /etc/nginx/sites-enabled/
    On RHEL/CentOS, files in conf.d are loaded automatically, so no symlink step is needed.
  5. Test the nginx configuration. Always check for syntax errors before reloading:
    sudo nginx -t
    If you see syntax is ok and test is successful, proceed. If there are errors, fix them before continuing.
  6. Reload nginx. Apply the changes without dropping connections:
    sudo systemctl reload nginx
    Or, if your system uses init.d:
    sudo service nginx reload
  7. Verify each subdomain. Open a browser and visit http://blog.example.com – you should see “Blog subdomain”. Then visit http://shop.example.com – you should see “Shop subdomain”. If you see the wrong content or a 404, check your DNS and the server block names.
  8. Add SSL (optional but recommended). If you have certificates, add the listen 443 ssl; lines and the ssl_certificate and ssl_certificate_key directives to each server block. You can also use a tool like Certbot to automate this for each subdomain.

Common Mistakes

  • Forgetting the trailing semicolon after root, server_name, or index – nginx will fail the config test.
  • Using the same server_name for two blocks – nginx will use the first one it finds, so the second subdomain won't work as expected.
  • Not creating the document root directory – nginx will return a 403 or 404 because it can't find files.
  • DNS not propagated – even with perfect nginx config, if the subdomain doesn't resolve to your server, you'll get a browser error.
  • Editing the default server block and adding your subdomain there instead of creating a new file – this can cause conflicts.

Tips

  • Use descriptive file names for server blocks, e.g., blog.example.com, not blog, to avoid confusion when you have many subdomains.
  • Keep all document roots under a common parent like /var/www/ for easier backups and permissions management.
  • Set a default server block (the first one nginx loads) to return 404 for unknown subdomains – this prevents attackers from using your IP to serve unintended content.
  • Use nginx -T (capital T) to dump the entire effective configuration and verify that your server blocks are being loaded as expected.
  • If you use a firewall, ensure ports 80 and 443 are open for the subdomains.

Troubleshooting

  • Subdomain shows the default nginx page or another site's content. This usually means your server block isn't being loaded. Check that the symlink exists in sites-enabled and that the server_name exactly matches the subdomain (including the trailing dot? No, it should not have a trailing dot). Also run sudo nginx -T and search for your server_name to confirm it's in the config.
  • Getting a 403 Forbidden. This often means nginx can't read the document root. Check permissions: the directory should be readable by the nginx user (usually www-data). Run sudo ls -la /var/www/blog and ensure the owner or group has read/execute permissions.
  • Getting a 404 Not Found. If the root is correct but the file doesn't exist, you'll get a 404. Check that your index.html is actually in the directory. Also verify the index directive in your server block – if you set it to index.php but only have HTML, it will fail.
  • Subdomain resolves but times out. This is usually a firewall issue. Check that your server's firewall allows HTTP/HTTPS traffic. On Ubuntu, sudo ufw status – if active, allow 80 and 443.
  • Changes to the server block don't take effect. Did you reload nginx after editing? Use sudo systemctl reload nginx (not restart, which is heavier but also works). Also check that you edited the correct file – the one in sites-available that is symlinked into sites-enabled.

Frequently Asked Questions

Can I use wildcard subdomains instead of listing each one?

Yes, you can use server_name *.example.com; in a single server block, and nginx will serve any subdomain from the same root. However, to have different document roots per subdomain, you must create separate server blocks for each – wildcards don't allow per-subdomain roots without using more advanced mapping.

What if I want to run PHP on one subdomain but not another?

That's fine – each server block is independent. In the block for the PHP subdomain, add the location ~ \.php$ block with the appropriate fastcgi_pass (e.g., to php-fpm). The other subdomain can simply serve static files.

Do I need to restart nginx or just reload?

Reload is sufficient for configuration changes – it gracefully reloads config without dropping connections. Restart is only needed for major changes like changing the nginx binary or modules.

My subdomain works with www but not without – why?

You need a server block for each hostname. If you only defined server_name www.example.com;, then requests to example.com won't match. Add a separate block or use server_name example.com www.example.com; in the same block.

Can I point two subdomains to the same document root?

Yes, simply use the same root directive in both server blocks, or use a single server block with both names in server_name (e.g., server_name blog.example.com shop.example.com;).

How do I redirect a subdomain to another domain or URL?

In the server block for that subdomain, use a return 301 https://target.com$request_uri; directive. This is common for moving old subdomains to a new domain.