How To

Nginx Server Blocks: Host Multiple Websites on Ubuntu

Managing multiple websites on a single Linux server becomes much easier with Nginx Server Blocks. This powerful Nginx feature allows administrators to host several domains on one server while maintaining separate configurations, document roots, logging, and security settings for each website.

Whether you’re running personal blogs, business websites, or development environments, server blocks provide a flexible and organized way to manage multiple domains on Ubuntu.

What Are Nginx Server Blocks?

Nginx Server Blocks are configuration units that define how Nginx handles requests for specific domains or websites. They work similarly to Apache Virtual Hosts and enable one server to serve different websites based on the requested domain name.

Each server block can have:

  • A dedicated document root
  • Custom log files
  • Independent SSL certificates
  • Separate security policies
  • Unique domain configurations

This separation improves website management and simplifies troubleshooting.

Prerequisites for Nginx Server Blocks

Before configuring server blocks, ensure the following requirements are met:

  • Ubuntu server with Nginx installed
  • Domain names pointing to your server’s public IP address
  • User account with sudo privileges
  • Proper DNS records configured

Once these prerequisites are in place, you can begin creating individual website environments.

Creating Website Directory Structures

Each hosted website should have its own document root directory. A common approach is to organize websites under the /var/www directory.

Example structure:

/var/www/├── site1.com/│   └── public_html├── site2.com/│   └── public_html

Create a directory for your domain:

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

Next, create a simple HTML file to verify that the site loads correctly.

<!DOCTYPE html><html><head><title>Welcome</title></head><body><h1>Website Successfully Configured</h1></body></html>

After creating the files, assign ownership to the Nginx web server user:

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

Configuring Nginx Server Blocks

The configuration files for Nginx Server Blocks are typically stored in:

/etc/nginx/sites-available/

Create a new configuration file for your domain:

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

Add the basic configuration:

server {    listen 80;    server_name example.com www.example.com;    root /var/www/example.com/public_html;    index index.html;    access_log /var/log/nginx/example.com.access.log;    error_log /var/log/nginx/example.com.error.log;}

This configuration instructs Nginx to serve files from the specified document root whenever requests arrive for the configured domain names.

Enable and Test Server Blocks

To activate the configuration, create a symbolic link inside the enabled sites directory:

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

Before reloading Nginx, validate the syntax:

sudo nginx -t

If the test succeeds, restart the service:

sudo systemctl restart nginx

Now open your domain in a browser to verify that the website loads correctly.

Benefits of Using Server Blocks

Using server blocks offers several advantages:

  • Efficient hosting of multiple domains
  • Easier website isolation and management
  • Better security customization
  • Simplified SSL deployment
  • Cleaner log organization

For administrators managing multiple web properties, server blocks are an essential Nginx feature.

Conclusion

Nginx Server Blocks provide a reliable and scalable way to host multiple websites on a single Ubuntu server. By creating separate document roots and dedicated configurations for each domain, administrators gain better control, security, and flexibility. Once configured properly, Nginx Server Blocks make managing multi-site environments significantly easier while maintaining excellent performance.

Cyber Defence

Recent Posts

Best OSINT Tools and Techniques 2026: From Collection to Verification

OSINT is not just about tools. In 2026, the best open-source intelligence work depends on…

21 minutes ago

Best Free Open Source OSINT Tools 2026: Create Your Own Recon Lab

Building an OSINT lab does not have to be expensive. In 2026, many of the…

33 minutes ago

Apache SSL Certificate Setup: Secure Ubuntu 20.04 with Let’s Encrypt

Website security is no longer optional. An Apache SSL Certificate helps encrypt data exchanged between…

39 minutes ago

Install Gradle Ubuntu: Complete Setup Guide for Developers

Gradle has become one of the most widely used build automation tools in modern software…

50 minutes ago

Install Memcached Ubuntu – Fast Caching Setup Guide

Modern web applications often rely on caching to deliver faster response times and reduce database…

58 minutes ago

Install Jenkins Ubuntu – Complete Jenkins Setup Guide

Install Jenkins Ubuntu systems quickly and efficiently to build powerful CI/CD pipelines for software development…

1 hour ago