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

# How to Host Multiple Websites with Nginx Server Blocks

> Host multiple websites on one Nginx server: create one server block per domain with its own server_name and root, enable it, run nginx -t and reload Nginx.

To host multiple websites on one Nginx server, create one server block per domain, each with its own `server_name` and `root`. On Ubuntu and Debian, save it in `/etc/nginx/sites-available/example.com` and link it into `/etc/nginx/sites-enabled/`; on AlmaLinux and Rocky, save it as `/etc/nginx/conf.d/example.com.conf`. Run `sudo nginx -t`, then `sudo systemctl reload nginx`.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 10 minutes per site | Ubuntu 22.04 / 24.04, Debian 12 / 13, AlmaLinux / Rocky 9 / 10 | Nginx installed, sudo |

## Before you start

* Install Nginx first: [install Nginx](https://help.freakhosting.com/portals/other/how-to-install-nginx.md).
* Point each domain's DNS `A` record at your server: [point a domain](https://help.freakhosting.com/portals/other/how-to-point-a-domain-to-your-server.md).
* Use a user with `sudo`. If you are `root`, leave out `sudo`.

A server block is Nginx's name for one site's configuration (Apache calls it a virtual host). Nginx reads the `Host` header of each request and picks the server block whose `server_name` matches. All your sites can share one IP address and port 80.

## How to add a website with a server block

This example adds `example.com`. Repeat it for each domain, such as `example.net`.

**1. Create the web root**

```bash theme={null}
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com
echo 'example.com works' > /var/www/example.com/html/index.html
```

**2. Write the server block**

Ubuntu / Debian: `/etc/nginx/sites-available/example.com`. AlmaLinux / Rocky: `/etc/nginx/conf.d/example.com.conf`.

```nginx theme={null}
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

**3. Enable the site (Ubuntu / Debian only)**

```bash theme={null}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
```

On AlmaLinux / Rocky every `*.conf` file in `conf.d` is loaded automatically.

**4. Test and reload**

```bash theme={null}
sudo nginx -t
sudo systemctl reload nginx
```

> [!NOTE]
>
> On AlmaLinux / Rocky with SELinux enforcing, label a new web root with `sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"` and `sudo restorecon -Rv /var/www/example.com`, or Nginx returns `403 Forbidden`.

## How to check it worked

Ask Nginx for each site from the server itself, using the `Host` header:

```bash theme={null}
curl -H "Host: example.com" http://127.0.0.1/
curl -H "Host: example.net" http://127.0.0.1/
```

Each command should return that site's own page. To see every server block Nginx loaded, run:

```bash theme={null}
sudo nginx -T | grep -E "server_name|root"
```

## Which site does Nginx show for an unknown domain?

If no `server_name` matches (for example, someone opens your bare IP), Nginx uses the **default server** for that port: the block marked `default_server`, or the first one it loaded. On Ubuntu and Debian that is the `default` site with the Nginx welcome page.

To stop unknown hostnames from showing one of your real sites, add a catch-all block and remove the `default` link:

```nginx theme={null}
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 444;
}
```

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

`return 444` closes the connection without a reply. Only one block per port may have `default_server`.

## How to disable or remove a site

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo rm /etc/nginx/sites-enabled/example.com
> sudo nginx -t && sudo systemctl reload nginx
> ```
>
> The file in `sites-available` stays, so you can enable it again later.

> [tab] AlmaLinux / Rocky
>
> ```bash theme={null}
> sudo mv /etc/nginx/conf.d/example.com.conf /etc/nginx/conf.d/example.com.conf.disabled
> sudo nginx -t && sudo systemctl reload nginx
> ```

## Frequently asked questions

### Nginx says "could not build server\_names\_hash". How do I fix it?

You have long or many domain names. In `/etc/nginx/nginx.conf`, inside `http { }`, set `server_names_hash_bucket_size 64;` (Ubuntu and Debian ship this line commented out), then run `sudo nginx -t`.

### Every domain shows the same site. Why?

The `server_name` does not match, so Nginx falls back to the default server. Check spelling, include both `example.com` and `www.example.com`, and confirm the file is enabled with `sudo nginx -T`.

### How do I add HTTPS to each site?

Run Certbot once per site or with several `-d` options: `sudo certbot --nginx -d example.com -d www.example.com`. See [free SSL with Certbot](https://help.freakhosting.com/portals/other/how-to-get-a-free-ssl-certificate-with-certbot.md).

### Can a server block run PHP or proxy to an app?

Yes. Add a PHP `location` ([LEMP stack](https://help.freakhosting.com/portals/other/how-to-install-a-lemp-stack.md)) or a `proxy_pass` ([reverse proxy](https://help.freakhosting.com/portals/other/how-to-set-up-nginx-as-a-reverse-proxy.md)) to the block.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Install Nginx](https://help.freakhosting.com/portals/other/how-to-install-nginx.md)
>
> Install and start Nginx.

> [card] [Free SSL with Certbot](https://help.freakhosting.com/portals/other/how-to-get-a-free-ssl-certificate-with-certbot.md)
>
> HTTPS for each domain.

> [card] [Nginx reverse proxy](https://help.freakhosting.com/portals/other/how-to-set-up-nginx-as-a-reverse-proxy.md)
>
> Send a domain to an app.

> [card] [Point a domain to your server](https://help.freakhosting.com/portals/other/how-to-point-a-domain-to-your-server.md)
>
> DNS records for each site.

***

> [!NOTE]
>
> **Checked against the official [nginx documentation on server names](https://nginx.org/en/docs/http/server_names.html) and [Ubuntu Server nginx guide](https://ubuntu.com/server/docs/how-to/web-services/configure-nginx/)** on 4 November 2026; server blocks, the catch-all block and `nginx -t` tested on Ubuntu 24.04 LTS (Nginx 1.24.0). Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


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