keepcalculator

06 Numbers and Powers

Nginx Multiple Sites: Separate Config Files Setup

Card No. 06·4552 · Updated · Standard formula, shown below

This guide shows you how to organize Nginx configuration for multiple websites by creating a separate config file for each site. Instead of cramming every server block into the main nginx.conf, you’ll use the sites-available and sites-enabled directories (or a similar pattern) to keep each site’s settings isolated, easy to edit, and simple to enable or disable. By the end, you’ll have a clean, maintainable setup where adding a new site takes minutes.

What You’ll Need

  • A Linux server (Ubuntu/Debian recommended) with Nginx already installed. If not, install it with sudo apt update && sudo apt install nginx.
  • Root or sudo access to the server.
  • Domain names (or subdomains) pointing to your server’s IP address. For testing, you can use localhost or edit your local hosts file.
  • Basic familiarity with the command line and editing files (using nano or vim).

Step-by-Step

  1. Understand the standard directory layout. Most Nginx installations on Debian/Ubuntu use /etc/nginx/sites-available for storing all site configs (even inactive ones) and /etc/nginx/sites-enabled for symlinks to the configs you actually want active. This separation lets you keep a backup of every site while controlling which ones are live.
  2. Create a config file for your first site. For example, for example.com, create a file in sites-available:
    sudo nano /etc/nginx/sites-available/example.com

    Inside, write a basic server block:

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

    Save and exit.

  3. Create the web root directory. Make the directory that will hold the site’s files and add a simple test file:
    sudo mkdir -p /var/www/example.com
    sudo echo "Hello from example.com" | sudo tee /var/www/example.com/index.html
  4. Enable the site by creating a symlink. Link your config from sites-available to sites-enabled:
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

    If you ever want to disable the site, just remove the symlink (do not delete the original file).

  5. Repeat for each additional site. Create a new config file in sites-available for each domain (e.g., example.org), set its own server_name and root, create its web directory, and symlink it into sites-enabled.
  6. Test the configuration. Before reloading Nginx, check that all configs are syntactically correct:
    sudo nginx -t

    You should see syntax is ok and test is successful. If there are errors, fix them before proceeding.

  7. Reload Nginx. Apply the changes without dropping connections:
    sudo systemctl reload nginx

    Or, if you prefer a full restart (brief downtime), use sudo systemctl restart nginx.

  8. Verify each site. In a browser, visit http://example.com and http://example.org. You should see the respective test pages. If you’re testing locally without real domains, edit your /etc/hosts file to point those names to 127.0.0.1.

Common Mistakes

  • Forgetting to create the symlink. If you only create the file in sites-available but don’t link it, Nginx won’t see it. Always check that the symlink exists in sites-enabled.
  • Using the same server_name for two sites. Nginx will use the first matching server block, which can cause one site to show the wrong content. Ensure each domain is unique.
  • Not setting a default server. If someone accesses your server by IP or an unknown domain, Nginx will use the first server block. It’s wise to set a default catch-all server that returns 404 or redirects.
  • Leaving the default site enabled. The default config at /etc/nginx/sites-enabled/default can conflict with your sites. Remove its symlink if you don’t need it.
  • Incorrect file permissions. Nginx needs read access to your web root. If you get 403 Forbidden, check that the directory and files are readable by the www-data user.

Tips

  • Use a consistent naming scheme. Name config files after the domain (e.g., example.com) to make it obvious which site each file belongs to.
  • Keep backups. Even when you disable a site, leave the config in sites-available so you can re-enable it later without rewriting everything.
  • Use SSL/TLS. For production, use Let’s Encrypt with certbot to get free certificates. It can automatically modify your server blocks to redirect HTTP to HTTPS.
  • Include common snippets. If many sites share settings (like gzip or cache headers), create a separate file in /etc/nginx/conf.d/ and include it inside each server block using include /etc/nginx/conf.d/common.conf;.
  • Test before reload. Always run nginx -t after editing any config. It’s fast and prevents accidental downtime.

Troubleshooting

Site shows the default Nginx page. This usually means your server block isn’t being used. Check that the symlink exists and that server_name matches the domain you’re accessing. Also ensure the default site is disabled.

404 Not Found. The server block is being used, but the root directory is empty or the file isn’t there. Verify the path in root and that the file exists with correct permissions.

403 Forbidden. Nginx can’t read the files. Check directory permissions: sudo chown -R www-data:www-data /var/www/example.com and set directories to 755, files to 644.

Configuration test fails. Run sudo nginx -t and read the error message. Common issues include missing semicolons, duplicate server names, or a missing closing brace. Fix the specific line it points to.

Changes don’t take effect. Did you reload Nginx after editing? Use sudo systemctl reload nginx. Also check that the symlink points to the correct file.

One site works, another doesn’t. Compare the working and non-working configs. Often it’s a typo in server_name or a missing symlink. Use sudo nginx -T to see the full effective configuration and spot conflicts.

Frequently Asked Questions

What is the difference between sites-available and sites-enabled?

sites-available holds all config files, even those not in use. sites-enabled contains symlinks to the configs you want active. This lets you keep a library of sites and toggle them on/off easily.

Can I use one config file for multiple domains?

Yes, you can define multiple server blocks in one file, or use a single server block with multiple server_name directives. However, separate files are cleaner and easier to manage, especially when sites have different settings.

How do I disable a site without deleting its config?

Remove the symlink from sites-enabled: sudo rm /etc/nginx/sites-enabled/example.com. The original file remains in sites-available for future use.

What if I don’t have a domain name?

You can still test by using localhost as the server_name, or by editing your local hosts file to map a fake domain (like test.local) to 127.0.0.1.

Is it safe to edit nginx.conf directly?

You can, but it’s better to leave the main config for global settings and use separate files for each site. This reduces the risk of breaking everything when you edit one site.

How do I add SSL to one of these sites?

Use sudo certbot --nginx -d example.com to automatically obtain and configure SSL. Certbot will modify the server block for you, adding the certificate paths and a redirect from HTTP to HTTPS.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →