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 very simple index pages.

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

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.

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 the following files so we can test our configuration.


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

<html>
    <head>
        <title>Site1</title>
    </head>
    <body>
        <h1>site1.com server block is working!</h1>
    </body>
</html>

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

<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.

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

Modify the file to look similar to below, ignoring comments: /etc/nginx/sites-available/site1.com:

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;
    }
}

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

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;
    }
}

default_server option can only be enabled in a single active server block.

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.

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/

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

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.

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

Now now 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