29.2 C
New York

Maximizing Web Server Efficiency with Nginx Server Blocks


Nginx, pronounced as “engine x,” stands as an open-source, high-performance HTTP and reverse proxy server renowned for its capacity to manage the load of some of the largest websites on the Internet. It serves as a versatile tool, functioning not only as a standalone web server but also as a load balancer, content cache, and reverse proxy for both HTTP and non-HTTP servers.

A pivotal component within Nginx configuration is the server block, a directive that delineates settings tailored to a specific domain, thereby enabling the hosting of multiple websites on a single server. Within each server block, administrators can define parameters such as the site’s document root (i.e., the directory housing the website files), establish distinct security policies, employ various SSL certificates, and more.

This guide elucidates the process of setting up Nginx server blocks on Ubuntu 22.04. It encompasses instructions on configuring multiple websites on a single server while tailoring each site’s settings to align with specific requirements.


Before proceeding, ensure the following prerequisites are met:

  • The domain name is configured to point to the public server IP.
  • Nginx is installed on the Ubuntu system.
  • Access to the server is granted either as root or through a user account with sudo privileges.

It’s noteworthy that in certain discussions concerning web servers, the term “Server Blocks” is akin to what is referred to as “Virtual Hosts” in Apache vernacular.

Creating the Directory Structure

The document root serves as the foundational directory wherein website files corresponding to a domain name are stored and served in response to requests. The document root can be designated to any preferred location. In the exemplified directory structure, the following hierarchy is utilized:

├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html

For each domain hosted on the server, its document root is set to /var/www/<domain_name>/public_html.

Commence by creating the root directory for the domain:

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

Subsequently, generate an index.html file and place it within the root directory of the domain. This file serves as the default page displayed upon accessing the domain URL in a web browser.


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

When executing commands as a sudo user, ownership of newly created files and directories defaults to the root user. To circumvent potential permission issues, it’s imperative to adjust ownership of the domain document root directory and all its contents to the Nginx user (www-data):

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

Creating a Server Block

Configuration files for Nginx server blocks on Ubuntu systems reside in the /etc/nginx/sites-available directory. Activation is achieved by creating symbolic links to the /etc/nginx/sites-enabled directory, which Nginx references during startup.

Utilize a text editor to craft the following server block file:


server {
    listen 80;

    server_name domain1.com www.domain1.com;

    root /var/www/domain1.com/public_html;

    index index.html;

    access_log /var/log/nginx/domain1.com.access.log;
    error_log /var/log/nginx/domain1.com.error.log;

Key elements of this configuration include:

  • server_name: Specifies domains corresponding to this server block.
  • root: Designates the directory from which Nginx serves domain files.
  • access_log, error_log: Define locations for log files.

While the configuration file can be named arbitrarily, employing the domain name typically proves beneficial.

To enable the new server block file, create a symbolic link from the file to the sites-enabled directory, which Nginx references during startup:

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

To validate the syntax of the Nginx configuration, conduct a test:

sudo nginx -t

A successful test yields output akin to the following:

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

Restart the Nginx service to enact the changes:

sudo systemctl restart nginx

Finally, to confirm the server block’s functionality, access http://domain1.com via your preferred web browser, whereupon you should encounter a page resembling the following:


This guide has elucidated the process of establishing Nginx server blocks to host multiple domains on a single Ubuntu server. Repeat the outlined steps to create additional server blocks for all requisite domains.

Should any issues arise during implementation, do not hesitate to provide feedback or queries for further assistance.

Related articles

Recent articles