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

Install and Configure Redis on Ubuntu 18.04: Remote Access Guide

Redis is an open-source, in-memory data structure store used as a database, cache, and message broker.…

2 hours ago

Upgrade Ubuntu 16.04 to 18.04 Bionic Beaver: Step-by-Step Guide

Ubuntu 18.04 LTS (Bionic Beaver) was released on April 26, 2018, with five years of official…

3 hours ago

Set Up Apache Virtual Hosts on Ubuntu 18.04: Complete Guide

Apache Virtual Hosts let you run multiple websites on a single server. Each site gets its…

3 hours ago

Install Django on Ubuntu 18.04: Python venv Setup Guide

Django is a free, open-source Python web framework built for developing secure, scalable, and maintainable web…

3 hours ago

Configure MySQL Master-Slave Replication on Ubuntu 18.04

MySQL replication is the process of automatically copying data from one database server to one or…

3 hours ago

Install Joomla on Ubuntu 18.04 with Apache: LAMP Stack Guide

Joomla is one of the most popular open-source content management systems in the world. It is…

1 day ago