- Posted on
- • Linux
Setting Up Nginx Server Blocks on Ubuntu 22.04
- Author
-
-
- User
- edan
- Posts by this author
- Posts by this author
-

Nginx, pronounced "engine x," is an open-source, high-performance HTTP and reverse proxy server renowned for supporting some of the most significant websites worldwide.
One of its powerful features is the ability to run multiple websites on a single server, known as server blocks in Nginx. This functionality allows system administrators to customize settings for individual domains, specifying document roots, security policies, and SSL certificates as needed.
This tutorial walks you through creating and configuring server blocks on an Ubuntu 22.04 system to host multiple domains efficiently.
Prerequisites
Before you get started, ensure you have met the following requirements:
- Your domain names are configured to point to the public IP address of your server.
- Nginx is installed and functional on your Ubuntu system.
- You have access to a user account with sudo privileges.
Keep in mind that while "server blocks" is the term used in Nginx, it is analogous to "virtual hosts" in Apache.
Creating the Directory Structure
Each domain hosted on your server will have a dedicated document root—the directory containing the website's files. For illustration, we'll use the following directory structure:
/var/www/
├── domain1.com
│ └── public_html
├── domain2.com
│ └── public_html
Each domain will have its document root set to /var/www/<domain_name>/public_html
.
To create this structure for domain1.com
, execute the following command:
sudo mkdir -p /var/www/domain1.com/public_html
Next, create a simple index.html
file in this directory. This file serves as the landing page displayed when visiting the domain:
/var/www/domain1.com/public_html/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to domain1.com</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
padding: 50px;
}
h1 {
color: #333;
}
</style>
</head>
<body>
<h1>Success! You have reached domain1.com home page!</h1>
</body>
</html>
Next, it's essential to adjust permissions to prevent potential access issues. Change the ownership of the document root directory and its files to the Nginx user (www-data
):
sudo chown -R www-data:www-data /var/www/domain1.com
Creating a Server Block
On Ubuntu, Nginx server block configurations are stored in the /etc/nginx/sites-available
directory and can be enabled by creating symbolic links in the /etc/nginx/sites-enabled
directory.
To create a new server block for domain1.com
, open your preferred text editor and create a new configuration file:
/etc/nginx/sites-available/domain1.com
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;
location / {
try_files $uri $uri/ =404;
}
}
Configuration Breakdown
server_name
: Specifies the domain names this configuration applies to.root
: The directory from which files are served.index
: The default file to serve when accessing the site.access_log
anderror_log
: Define paths for storing specific logs.
After saving the configuration file, create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/
Next, check the syntax of your Nginx configuration files to ensure everything is in order:
sudo nginx -t
If the configuration is correct, you will see output similar to:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
To apply the changes, restart the Nginx service:
sudo systemctl restart nginx
To verify that your server block is operational, open a web browser and navigate to http://domain1.com
. You should see the welcome message from your index.html
file.
Creating Additional Server Blocks
You can replicate the above steps for domain2.com
or any other domain by following the same directory structure, file creation, and server block configuration processes. Each domain will have its tailored settings without interference.
Conclusion
Creating Nginx server blocks on Ubuntu 22.04 allows you to host multiple websites on a single server efficiently. With this guide, you can now establish distinct configurations for different domains, setting the stage for a scalable and organized web hosting environment. Happy hosting!