Set Up Multiple Server Blocks with Nginx
Last Updated: 02 October 2021
Introduction
Here are the instructions on how to set up multiple server blocks when using a Nginx web server. This allows hosting multiple websites on a single droplet on DigitalOcean.
Prerequisites
An existing DigitalOcean droplet with sudo user set up.
Step 1 — Create the Folder and Set Permissions
SSH into your Digital Ocean droplet.
Create the necessary directories for our test sites - test1.com and test2.com.
sudo mkdir -p /var/www/test1.com/html
sudo mkdir -p /var/www/test2.com/html
NOTE: mkdir with the -p tag will create the directory and all missing parent directories.
- Transfer ownership to the regular user.
sudo chown -R $USER:$USER /var/www/test1.com/html
sudo chown -R $USER:$USER /var/www/test2.com/html
NOTE:
The-R
tag will go through directories recursively, changing the ownership for each file.
The$USER
is the environmental variable for the user account that we are currently signed in on.
The$USER:$USER
will change the ownership of the files along with the file group.
- Update the permissions of the web root.
sudo chmod -R 755 /var/www
NOTE: 755 means read and execute access for everyone and write access for the owner of the file.
Step Two — Create Sample Pages for Each Site
- Create an
index.html
file in the previously created folder and add some sample content.
sudo vim /var/www/test1.com/html/index.html
index.html
<html>
<body>
<h1>First Website</h1>
<p>Test Paragraph.</p>
</body>
</html>
NOTE: To save the file and exit the Vim editor, press Esc to switch to normal mode, type
:wq
and hit Enter.
- Create a second
index.html
file similar to the first.
sudo vim /var/www/test2.com/html/index.html
index.html
<html>
<body>
<h1>Second Website</h1>
<p>Test Paragraph.</p>
</body>
</html>
Step Three — Create Server Block Files for Each Domain
Use the -v
tag to check for the Nginx version and installation. Installed Nginx if it's not already installed.
nginx -v
sudo apt-get update
sudo apt-get install nginx
- Copy the default config file for our configurations.
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/test1.com
- Open the new file and update the configuration inside to look like what's below.
sudo vim /etc/nginx/sites-available/test1.com
test1.com
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/test1.com/html;
index index.html index.htm;
server_name test1.com www.test1.com;
location / {
try_files $uri $uri/ =404;
}
}
NOTE:
listen
tells Nginx the port where it should listen for HTTP connections.
root
points to the site’s document root that you created.
server_name
is set to match domain name. We can additionally add any aliases that we want to match.
- Now do the same for the second server block.
sudo cp /etc/nginx/sites-available/test1.com /etc/nginx/sites-available/test2.com
sudo vim /etc/nginx/sites-available/test2.com
Your file should look something like this:
test2.com
server {
listen 80;
listen [::]:80;
root /var/www/test2.com/html;
index index.html index.htm;
server_name test2.com www.test2.com;
location / {
try_files $uri $uri/ =404;
}
}
NOTE: Remove the
default_server
from thelisten
directives. Since only one configuration can be set as the default. Furthermore, remove theipv6only=on
option, as it can only be specified once per address/port pair.
Step Four — Enable your Server Blocks
- Create symbolic links from these files to the
sites-enabled
directory.
sudo ln -s /etc/nginx/sites-available/test1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/test2.com /etc/nginx/sites-enabled/
NOTE: Nginx reads from
sites-enabled
during startup. These files are now in the enabled directory. However, the default server block file we used as a template is also enabled currently and will conflict with our file that has thedefault_server
parameter set.
- Remove the symbolic link for default so it won't be read.
sudo rm /etc/nginx/sites-enabled/default
- Restart Nginx to enable your changes.
sudo service nginx restart
Nginx should now be serving both of your domain names.