Skip to main content

Web servers and networking · Intermediate · 11 min

How to Host Multiple Websites on One Server with Nginx

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · Nginx 1.30

You will run two separate websites on one Ubuntu 26.04 server by giving each its own Nginx server block, decide what unknown names receive, and check every site before DNS points at it.

On this page

A web server at one IP address can serve any number of sites. Every HTTP request carries a Host header with the name the visitor typed, and Nginx uses it to choose a server block: a self-contained description of one site. This tutorial sets up two sites, example.com and example.org, each with its own folder, configuration file and logs. Both names are reserved for examples, so swap in your own.

The paths here are those of the Nginx 1.30 package from nginx.org, which reads every *.conf file in /etc/nginx/conf.d/. Ubuntu's own nginx package works differently, with sites-available and sites-enabled. Install Nginx first by following /tutorials/install-nginx-on-ubuntu-26-04.

Step 1 · Give each site a folder

Keep the files of each site apart under /var/www, so you can back up, move or delete one without touching the other.

Bash
sudo mkdir -p /var/www/example.com/html /var/www/example.org/html
echo '<h1>Welcome to example.com</h1>' | sudo tee /var/www/example.com/html/index.html
echo '<h1>Welcome to example.org</h1>' | sudo tee /var/www/example.org/html/index.html

The Nginx worker processes only need to read these files. Make yourself the owner so you can edit them, and leave them readable by everyone:

Bash
sudo chown -R "$USER":"$USER" /var/www/example.com /var/www/example.org
sudo chmod -R 755 /var/www

Check it worked

Bash
cat /var/www/example.com/html/index.html /var/www/example.org/html/index.html

You see one heading for each site.

Step 2 · Describe each site in its own file

Write one file per site in /etc/nginx/conf.d/. The names must end in .conf or Nginx will not read them.

Bash
sudo tee /etc/nginx/conf.d/example.com.conf > /dev/null <<'CONF'
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;
    }
}
CONF
sudo tee /etc/nginx/conf.d/example.org.conf > /dev/null <<'CONF'
server {
    listen 80;
    listen [::]:80;
    server_name example.org www.example.org;

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

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

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

The parts of a block, in order:

  • Two listen lines accept IPv4 and IPv6 connections on port 80.
  • server_name lists the names that belong to this site. Nginx matches the request's Host header against them.
  • root is the folder the files come from, and index names the file served for a folder.
  • The log lines give each site its own access and error log, so a problem on one site is easy to find.
  • try_files looks for the file, then for a folder, and answers 404 when neither exists.

Step 3 · Decide what unknown names receive

If a request names a host that no block claims, Nginx falls back to a default server for that port. Unless you choose one, it is whichever block Nginx read first, so a visitor who types your IP address would land on one of your sites by accident. Pick the behaviour yourself. This block closes the connection without an answer:

Bash
sudo tee /etc/nginx/conf.d/00-default.conf > /dev/null <<'CONF'
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 444;
}
CONF

return 444 is specific to Nginx: it drops the connection and sends nothing back. That suits the automated scanners that probe bare IP addresses.

The package also ships a default.conf, which serves a welcome page. Remove it, so that page does not answer for your bare IP address, and the two files agree on which server is the default:

Bash
sudo rm -f /etc/nginx/conf.d/default.conf

Troubleshooting

  • conflicting server name "example.com" on 0.0.0.0:80, ignored: two blocks claim the same name. Find them with grep -r server_name /etc/nginx/.
  • duplicate default server for 0.0.0.0:80: two blocks are marked default_server. Keep one.
  • 403 Forbidden on a site: Nginx cannot read its files. Inspect the whole path with namei -l /var/www/example.com/html/index.html; every folder needs x for others, and the file needs r.

Step 4 · Apply the configuration

Check the files first. If there is a mistake, nginx -t names the file and the line.

Bash
sudo nginx -t

It ends with syntax is ok and test is successful. Then tell the running server to read them. A reload keeps current connections alive.

Bash
sudo systemctl reload nginx

Step 5 · Try each site before DNS exists

curl can send any Host header you like, so you do not need DNS to check which block answers.

Bash
curl -s -H 'Host: example.com' http://127.0.0.1/
curl -s -H 'Host: example.org' http://127.0.0.1/

The first prints <h1>Welcome to example.com</h1> and the second <h1>Welcome to example.org</h1>. Now use a name you did not configure:

Bash
curl -sS -H 'Host: unknown.test' http://127.0.0.1/

curl reports Empty reply from server. That is the catch-all closing the connection, as intended.

Check it worked

Each site answers with its own page and unknown names get nothing. Each site also writes to its own log:

Bash
sudo cat /var/log/nginx/example.com.access.log

The log has a line for the example.com request, and none for the other site.

When you are ready to go live, create an A record for each name, and for each www name, that points at the server's public IP address. /tutorials/dns-records-explained-a-cname-mx-txt explains the records. Then open both sites in a browser.

Next steps

  • Serve both sites over HTTPS: /tutorials/free-https-with-lets-encrypt-and-nginx.
  • Send one of the sites to an application instead of files: /tutorials/nginx-reverse-proxy-for-a-node-app.
  • Add protective headers to each site: /tutorials/add-security-headers-in-nginx.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.