> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Configure Multiple Sites

> Guide to configure Nginx name based virtual hosting to serve multiple domains from one server using separate server blocks, document roots, symlinks in sites enabled, and curl testing.

In this guide you'll configure Nginx to serve two separate websites from the same server using distinct server blocks (virtual hosts) and Host header routing. This is commonly called name-based virtual hosting and lets one IP address handle multiple domains.

![A presentation slide that says "Configure Multiple Sites" with a large turquoise shape on the right containing the word "Demo." A small "© Copyright KodeKloud" notice appears at the bottom left.](https://mintcdn.com/kodekloud-c4ac6d9a/ij3FNIPjp1w0Myd0/images/Nginx-for-Beginners/Intermediate-Config/Demo-Configure-Multiple-Sites/configure-multiple-sites-demo-slide.jpg?fit=max\&auto=format\&n=ij3FNIPjp1w0Myd0\&q=85\&s=28f608a3e091d193bc01d6d5475a80ec)

Overview

* A client (browser or CLI such as `curl`) sends an HTTP request to Nginx.
* Nginx inspects the `Host` header and selects the matching server block (for example, `example1` or `example2`).
* Each server block serves files from its own document root (e.g. `/var/www/example1` and `/var/www/example2`).

Prerequisites

* You have a Linux server (Ubuntu/Debian recommended) with Nginx installed.
* You can run commands with `sudo`.
* Basic familiarity with editing files (e.g. `nano` or `vim`).

Quick reference — important file locations

| Purpose | Path / Example |
| - | - |
| Site configurations (available) | `/etc/nginx/sites-available/` |
| Enabled sites (active via symlink) | `/etc/nginx/sites-enabled/` |
| Nginx main config | `/etc/nginx/nginx.conf` |
| Document roots | `/var/www/<site>` (e.g. `/var/www/example1`) |
| Test config and reload | `sudo nginx -t` then `sudo nginx -s reload` |

Step 1 — Prepare the environment

1. Remove the default enabled site so it does not interfere with this demo:

```bash theme={null}
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -s reload
```

2. Verify no service responds on port 80:

```bash theme={null}
curl localhost
# Expected output (if nothing is listening on :80):
# curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused
```

Step 2 — Create two site configuration files

1. Copy the default Nginx site to create `example1` and then duplicate it for `example2`:

```bash theme={null}
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example1
sudo cp /etc/nginx/sites-available/example1 /etc/nginx/sites-available/example2
```

2. Edit `/etc/nginx/sites-available/example1` to serve the first site. A minimal server block looks like:

```nginx theme={null}
server {
    listen 80;

    root /var/www/example1;
    index index.html index.htm;

    server_name www.example1.com;

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

3. Edit `/etc/nginx/sites-available/example2` for the second site:

```nginx theme={null}
server {
    listen 80;

    root /var/www/example2;
    index index.html index.htm;

    server_name www.example2.com;

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

Step 3 — Create document roots and sample pages
Create directories and simple `index.html` files for each site so we can verify routing:

```bash theme={null}
sudo mkdir -p /var/www/example1 /var/www/example2

# Create index.html for example1
sudo tee /var/www/example1/index.html > /dev/null <<'HTML'
<h1> Example 1!!! </h1>
HTML

# Create index.html for example2
sudo tee /var/www/example2/index.html > /dev/null <<'HTML'
<h1> Example 2!!! </h1>
HTML
```

Step 4 — Enable the sites (create symbolic links)
Enable each site by linking from `sites-available` into `sites-enabled`:

```bash theme={null}
sudo ln -s /etc/nginx/sites-available/example1 /etc/nginx/sites-enabled/example1
sudo ln -s /etc/nginx/sites-available/example2 /etc/nginx/sites-enabled/example2
```

Step 5 — Validate and reload Nginx
Always test Nginx configuration before reloading:

```bash theme={null}
sudo nginx -t
# Example successful messages:
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
sudo nginx -s reload
```

Step 6 — Test with curl using the Host header
Use `curl` to simulate requests for each domain by setting the `Host` header:

```bash theme={null}
curl --header "Host: www.example1.com" localhost
curl --header "Host: www.example2.com" localhost
# -> <h1> Example 2!!! </h1>
```

If the `Host` header does not match any `server_name`, Nginx will serve the default server for that `listen` socket — typically the first enabled server block for that address:port (often determined by the alphabetical order of files in `/etc/nginx/sites-enabled`):

```bash theme={null}
curl --header "Host: anything.com" localhost
# -> <h1> Example 1!!! </h1>   # example1 becomes the default in this demo
```

Note about the default server selection

If a request's `Host` header doesn't match any `server_name`, Nginx uses the default server for that `listen` socket. The default is the first server block for that address:port — commonly the first file (alphabetically) in `/etc/nginx/sites-enabled/`. To explicitly set a default server, use the `default_server` flag on the `listen` directive (for example `listen 80 default_server;`).

Disabling sites temporarily

* To disable a site without deleting its configuration, remove the symlink from `/etc/nginx/sites-enabled/` and reload Nginx:

```bash theme={null}
sudo rm /etc/nginx/sites-enabled/example2
sudo nginx -s reload
```

This keeps the file in `/etc/nginx/sites-available/` for later reuse.

Alternative: combine multiple server blocks in one file (not recommended)

* You can place multiple `server { ... }` blocks in a single file and enable that one file with a single symlink. Example combined file:

```nginx theme={null}
server {
    listen 80;
    root /var/www/example1;
    index index.html index.htm;
    server_name www.example1.com;
    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80;
    root /var/www/example2;
    index index.html index.htm;
    server_name www.example2.com;
    location / {
        try_files $uri $uri/ =404;
    }
}
```

Warning about combining server blocks

Keeping each site in its own file under `/etc/nginx/sites-available/` and enabling them with separate symlinks reduces risk. If a single combined file is accidentally edited or removed, multiple sites could be taken down at once.

Summary — best practices

* Create one server block per site and set a unique `server_name`.
* Point each server block to its own document `root`.
* Enable sites with symlinks in `/etc/nginx/sites-enabled/`.
* Always validate config with `sudo nginx -t` then reload with `sudo nginx -s reload`.
* Prefer separate files per site for isolation and safer management.

Links and references

* [Nginx documentation — Server Names](https://nginx.org/en/docs/http/server_names.html)
* [Nginx configuration basics](https://nginx.org/en/docs/beginners_guide.html)

> **[Watch Video](https://learn.kodekloud.com/user/courses/nginx-for-beginners/module/c78ff9cb-c15d-4f85-92fc-abee5ed98b20/lesson/db98f13e-91c5-43d1-9400-fd40daf84b42)**

> **[Practice Lab](https://learn.kodekloud.com/user/courses/nginx-for-beginners/module/c78ff9cb-c15d-4f85-92fc-abee5ed98b20/lesson/c74f891f-6063-47cc-8144-ada26fef3e78)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.