Cybersecurity Updates & Tools

Nginx Server Blocks: Host Multiple Sites on Ubuntu

Running several websites on one Linux server is easier than many administrators think. With Nginx Server Blocks, you can host multiple domains using a single Ubuntu machine while keeping each website isolated with its own configuration, logs, and web files.

This feature works similarly to Apache virtual hosts and is commonly used in VPS hosting, cloud deployments, and development environments.

What Are Nginx Server Blocks?

Nginx Server Blocks allow Nginx to serve different websites based on the requested domain name. Each server block contains its own settings, including:

  • Domain name
  • Document root
  • Access and error logs
  • SSL configuration
  • Security rules

This setup makes server management cleaner and more scalable, especially when hosting multiple projects on one server.

Before starting, ensure your domains point to your server’s public IP address and Nginx is already installed on Ubuntu.

Creating Directory Structures for Nginx Server Blocks

The first step is organizing website files properly. Most administrators create separate directories inside /var/www/ for every domain.

A common structure looks like this:

/var/www/domain1.com/public_html/var/www/domain2.com/public_html

Each domain receives its own public_html directory where website content is stored.

You can create the folders using:

sudo mkdir -p /var/www/domain1.com/public_html

After creating the directory, add a simple index.html test page to confirm the website loads correctly later.

It’s also important to set proper ownership permissions:

sudo chown -R www-data:www-data /var/www/domain1.com

This ensures Nginx can access and serve the files without permission issues.

Configure Nginx Server Blocks on Ubuntu

Nginx stores website configurations inside two main directories:

  • /etc/nginx/sites-available/
  • /etc/nginx/sites-enabled/

Create a new configuration file for your domain:

sudo nano /etc/nginx/sites-available/domain1.com

Inside the file, define the server settings:

server {    listen 80;    server_name domain1.com www.domain1.com;    root /var/www/domain1.com/public_html;    index index.html;    location / {        try_files $uri $uri/ =404;    }}

This tells Nginx which domain to serve and where the website files are located.

Next, enable the configuration by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/

Before applying changes, test the configuration:

sudo nginx -t

If no errors appear, reload Nginx:

sudo systemctl reload nginx

Your domain should now load successfully in a browser.

Hosting Multiple Websites

Adding additional websites follows the exact same process. Create another directory, add website files, generate a new server block configuration, and enable it.

This approach keeps every website separated while sharing the same server resources efficiently.

For production servers, administrators often disable the default Nginx welcome page to prevent conflicts with custom domains.

Common Troubleshooting Tips

If the default Nginx page appears instead of your site, verify the server_name values and ensure the symbolic link exists in sites-enabled.

If Nginx fails to start, check whether another service is already using port 80:

sudo ss -tlnp | grep :80

You can also use:

sudo nginx -t

to identify syntax errors in configuration files.

Conclusion

Using Nginx Server Blocks is one of the best ways to host multiple domains on a single Ubuntu server. The setup improves organization, simplifies website management, and allows each project to maintain separate configurations and logs. Whether you manage personal projects or production websites, Nginx Server Blocks provide a scalable and efficient hosting solution.