Cybersecurity Updates & Tools

Set Up Nginx Server Blocks on Ubuntu 18.04: Host Multiple Sites

Nginx server blocks let you run more than one website on a single server. Each block tells Nginx which domain to listen for and where to find that site’s files. You can give each site its own document root, separate log files, and a different SSL certificate.

If you have used Apache before, server blocks are the Nginx equivalent of virtual hosts. The idea is the same — one server, many sites, each with its own settings.

This guide shows you how to set up Nginx server blocks on Ubuntu 18.04, using example.com as the demo domain.

Prerequisites: Nginx installed, a domain name pointing to your server’s public IP address, and sudo access.

Plan the Directory Structure

Nginx needs to know where each site’s files live. This location is called the document root. A clean way to manage multiple sites is to give each domain its own folder under /var/www/.

The structure looks like this:

/var/www/├── example.com/│   └── public_html/

Each domain gets its own directory. This keeps files organized and makes it straightforward to manage permissions per site.

Create the document root for your domain:

bashsudo mkdir -p /var/www/example.com/public_html

Create a test page so you can confirm the setup works:

/var/www/example.com/public_html/index.html

html<!DOCTYPE html><html lang="en">  <head>    <meta charset="utf-8">    <title>Welcome to example.com</title>  </head>  <body>    <h1>Success! example.com home page!</h1>  </body></html>

Set the correct ownership so Nginx can read the site files:

bashsudo chown -R www-data: /var/www/example.com

Create the Server Block Configuration File

Nginx stores site configuration files in /etc/nginx/sites-available/. A site only goes live when you link its config file into the /etc/nginx/sites-enabled/ directory.

Create a new config file for your domain:

/etc/nginx/sites-available/example.com

nginxserver {    listen 80;    listen [::]:80;    root /var/www/example.com/public_html;    index index.html;    server_name example.com www.example.com;    access_log /var/log/nginx/example.com.access.log;    error_log /var/log/nginx/example.com.error.log;    location / {        try_files $uri $uri/ =404;    }}

Here is what the key directives do:

  • server_name — tells Nginx which domain this block responds to
  • root — points to the folder containing the site’s files
  • access_log and error_log — write traffic and error data to separate files, which makes troubleshooting easier when hosting multiple sites
  • try_files — serves the requested file or returns a 404 error if it does not exist

Enable, Test, and Restart Nginx

Activate the server block by creating a symlink from sites-available to sites-enabled:

bashsudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Using a symlink instead of copying the file means you only maintain one version. You can disable a site at any time by removing the symlink without touching the original config file.

Test the Nginx configuration for syntax errors before restarting:

bashsudo nginx -t

Output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx to apply the new configuration:

bashsudo systemctl restart nginx

Open http://example.com in your browser. You should see the test page load.

Your first Nginx server block is now active on Ubuntu 18.04. To host additional domains, repeat the same steps for each one — create its directory, write a config file, and enable it with a symlink. When you are ready to add HTTPS, follow the guide on securing Nginx with a free Let’s Encrypt SSL certificate. Leave a comment below if you run into any issues.