Host Multiple Websites on One Server with Nginx: A Step-by-Step Guide

Mohi uddinMohi uddin
4 min read

leverages Nginx Server Blocks to handle requests for different domains, all from a single server. This is efficient and perfect for managing several projects without needing multiple virtual machines.

Today, I'll walk you through setting up Nginx to host two distinct websites (e.g., your_domain1.com and your_domain2.com) using a single Nginx configuration file.


Why Nginx for Multiple Sites?

Nginx acts as a reverse proxy and a powerful web server. Its server blocks allow it to inspect incoming requests' Host headers (which contain the domain name) and serve content from the correct website directory. This means you can run several web applications or static sites on the same server, all accessible via their unique domain names.


Let's Get Started!

Before diving in, make sure Nginx is installed on your Linux server. Crucially, your domain names (your_domain1.com and your_domain2.com) must have their DNS A/AAAA records pointing to your server's public IP address.

Step 1: Create Document Root Directories

First, create dedicated directories for each website's files. This keeps things organized.

sudo mkdir -p /var/www/website1.com/html
sudo mkdir -p /var/www/website2.com/html

These directories will hold all your website's HTML, CSS, JavaScript, and image files.

Step 2: Create Sample HTML Files (Optional, for Testing)

To confirm everything works correctly, let's place a simple index.html file in each directory.

For website1.com:

sudo nano /var/www/website1.com/html/index.html

Add this content:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Website 1!</title>
</head>
<body>
    <h1>Hello from Website 1!</h1>
    <p>This is the content for website1.com.</p>
</body>
</html>

For website2.com:

sudo nano /var/www/website2.com/html/index.html

Add this content:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Website 2!</title>
</head>
<body>
    <h1>Greetings from Website 2!</h1>
    <p>This is the content for website2.com.</p>
</body>
</html>

Save and exit nano for both files.

Step 3: Set Permissions

Nginx needs proper permissions to read your website files.

sudo chown -R www-data:www-data /var/www/website1.com
sudo chown -R www-data:www-data /var/www/website2.com
sudo chmod -R 755 /var/www/website1.com
sudo chmod -R 755 /var/www/website2.com

This ensures the www-data user (which Nginx typically runs as) can access the files.

Step 4: Create a Single Nginx Configuration File

This is the core step! We'll create one file with separate server blocks for each domain.

sudo nano /etc/nginx/sites-available/all-my-sites.conf

Paste the following configuration. Remember to replace your_domain1.com and your_domain2.com with your actual domain names!

# /etc/nginx/sites-available/all-my-sites.conf

# Server block for your_domain1.com
server {
    listen 80;
    listen [::]:80; # Listen on IPv6 as well
    server_name your_domain1.com www.your_domain1.com; # Your primary domain and www

    root /var/www/website1.com/html; # Path to your website1 files
    index index.html index.htm; # Default files to serve

    location / {
        try_files $uri $uri/ =404; # Serve static files, or return 404
    }

    # Optional: Log files for this specific site
    access_log /var/log/nginx/website1.com_access.log;
    error_log /var/log/nginx/website1.com_error.log warn;
}

# Server block for your_domain2.com
server {
    listen 80;
    listen [::]:80; # Listen on IPv6 as well
    server_name your_domain2.com www.your_domain2.com; # Your second domain and www

    root /var/www/website2.com/html; # Path to your website2 files
    index index.html index.htm; # Default files to serve

    location / {
        try_files $uri $uri/ =404; # Serve static files, or return 404
    }

    # Optional: Log files for this specific site
    access_log /var/log/nginx/website2.com_access.log;
    error_log /var/log/nginx/website2.com_error.log warn;
}

Save and exit the file.

Step 5: Enable the Single Server Block File

Nginx loads configurations from the sites-enabled directory. We'll create a symbolic link to activate our new combined configuration.

sudo ln -s /etc/nginx/sites-available/all-my-sites.conf /etc/nginx/sites-enabled/

Important: It's crucial to disable or remove any conflicting default Nginx configurations or previous individual site setups in sites-enabled. This prevents unintended behavior or errors.

sudo rm /etc/nginx/sites-enabled/default
# Remove any other old site configs if you created them previously
# sudo rm /etc/nginx/sites-enabled/website1.com.conf
# sudo rm /etc/nginx/sites-enabled/website2.com.conf

Step 6: Test Nginx Configuration

Always test your Nginx configuration for syntax errors before reloading. This prevents service outages.

sudo nginx -t

You should see:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If you encounter any errors, carefully review your all-my-sites.conf file for typos like missing semicolons or incorrect paths.

Step 7: Reload Nginx

Once the test is successful, reload Nginx to apply your new configuration.

sudo systemctl reload nginx

Final Thoughts

You've now successfully configured Nginx to host two separate websites from a single server using one concise configuration file! When a user visits your_domain1.com, Nginx will serve the content from /var/www/website1.com/html, and similarly for your_domain2.com.

Next Steps (Highly Recommended):

  • HTTPS (SSL/TLS): For production environments, always secure your websites with HTTPS. You can obtain free SSL certificates from Let's Encrypt using Certbot, which can automate Nginx configuration updates for you.

  • Firewall: Ensure your server's firewall (e.g., UFW, Firewalld) allows incoming traffic on port 80 (HTTP) and, once you add SSL, port 443 (HTTPS).

This setup is efficient and scalable, making Nginx an excellent choice for managing multiple web properties!


0
Subscribe to my newsletter

Read articles from Mohi uddin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohi uddin
Mohi uddin