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
sudoaccess. - 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.
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/wwwPermissions 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:
<html>
<head>
<title>Site1</title>
</head>
<body>
<h1>site1.com server block is working!</h1>
</body>
</html>Edit /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.comUpdate /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;
}
}Update /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;
}
}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/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.
sudo systemctl restart nginxTo 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/defaultNow we have 2 server blocks enabled:
site1.comwhich will respond tosite1.comandwww.site1.comsite2.comwhich will respond tosite2.comandwww.site2.com
Keep reading
Copy from a Protected Google Sheet/Doc
Google Docs/Sheets give users ability to 'lock' their content. This makes files viewable but no editing or copying will work. Here is how to copy from a protected Google Sheet/Doc.
