Multisite Nginx Server

How to set up multiple sites on one Nginx server.

Server blocks can be used to configure and host more than one domain on a single server.

Prerequisites

Make sure Nginx is installed and can be started without any issues. For this guide we will be:

  • using a non-root user with sudo access.
  • setting up Nginx to run two sites, site1.com and site2.com with one page (index.html) each.

We will also be assuming the DNS has been configured already.

A simple A record pointing to the server’s IP address is all that is needed for this guide.

Set Up Document Root Directories and Their Index Pages

Nginx has one server block enabled by default and it is configured to serve documents from /var/www/html. For each site we need to create directories with specific permissions and ownership as follows.

1
2
3
4
5
6
7
sudo mkdir -p /var/www/site1.com
sudo mkdir -p /var/www/site2.com

sudo chown -R $USER:$USER /var/www/site1.com
sudo chown -R $USER:$USER /var/www/site2.com

sudo chmod -R 755 /var/www

Permissions and ownership of files/folders should be adjusted based on the technology and configuration.

Now that the directories have been created, we can create index.html files so we can test our configuration.

Edit /var/www/site1.com/index.html:

1
2
3
4
5
6
7
8
<html>
    <head>
        <title>Site1</title>
    </head>
    <body>
        <h1>site1.com server block is working!</h1>
    </body>
</html>

Edit /var/www/site2.com/index.html:

1
2
3
4
5
6
7
8
<html>
    <head>
        <title>Site2</title>
    </head>
    <body>
        <h1>site2.com server block is working!</h1>
    </body>
</html>

Create Server Block Files

Nginx contains one server block called default which we will use as a template.

1
2
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site1.com
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site2.com

Update /etc/nginx/sites-available/site1.com:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/site1.com;
    index index.html index.htm index.nginx-debian.html;

    server_name site1.com www.site1.com;

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

Update /etc/nginx/sites-available/site2.com:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
server {
    listen 80;
    listen [::]:80;

    root /var/www/site2.com;
    index index.html index.htm index.nginx-debian.html;

    server_name site2.com www.site2.com;

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

Enable Server Blocks and Restart Nginx

There are two directories under /etc/nginx/ that hold the server block files. One is for available sites and the second is for enabled sites. The server block files we created in the step above are in the available-sites folder so now we need to create links for them to enable them.

1
2
sudo ln -s /etc/nginx/sites-available/site1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2.com /etc/nginx/sites-enabled/

It is good practice to enable sites by creating links in the enabled-sites folder so that it is easier to manage and remove sites without losing the configuration files.

Now that our server block files are linked to the enabled-sites folder, we can test our Nginx configuration and restart Nginx.

1
sudo systemctl restart nginx

To test the nginx configuration, run sudo nginx -t.

Disable default Server Block and Test Configuration

There should be three files in /etc/nginx/sites-available directory and links to the same files in /etc/nginx/sites-enabled. We need to remove the default server block from the enabled sites folder.

1
sudo rm -f /etc/nginx/sites-enabled/default

Now we have 2 server blocks enabled:

  • site1.com which will respond to site1.com and www.site1.com
  • site2.com which will respond to site2.com and www.site2.com