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

What Is a Data Protection Platform and Why Your Organization Needs One

A data protection platform is a unified system that helps organizations discover, classify, monitor, and…

2 weeks ago

How Swiss Privacy Rules Affect AI Companies Under GDPR

AI companies operating in or around Switzerland face a compliance challenge that most legal teams…

2 weeks ago

Swiss FADP vs EU GDPR: Key Differences for AI and Data Privacy

Two major data privacy laws now govern how organizations handle personal data across Europe; the…

2 weeks ago

Kali Linux Commands Cheat Sheet: Complete Quick Reference

This cheat sheet covers the essential Kali Linux commands every pentester and ethical hacker uses…

1 month ago

What I Wish I Knew Before Learning Malware Analysis and Reverse Engineering

When I first started learning malware analysis and reverse engineering, I thought the hardest part…

2 months ago

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 months ago