Choose a different version or distribution
Introduction
Before we begin talking about how to set up Nginx Server Blocks on Ubuntu 20.04, let's briefly understand – What is Nginx Server Blocks?
Nginx server blocks, also known as virtual hosts, are a powerful feature of the Nginx web server. They allow you to host multiple websites or applications on a single server by defining separate configurations for each domain or subdomain.
With Nginx server blocks, you can efficiently manage and control the traffic to your websites, improving performance and scalability. Whether you're a small business or a large enterprise, Nginx server blocks provide the flexibility and reliability you need for your web hosting needs.
In this tutorial, we will explain how to set up Nginx server blocks on Ubuntu 20.04.
Advantages of Nginx server blocks
- Efficient website hosting: Nginx server blocks allow you to host multiple websites or applications on a single server, optimizing resource usage.
- Improved performance: With separate configurations for each domain, Nginx server blocks enhance website speed and response time.
- Enhanced scalability: Easily scale your hosting environment by adding or removing server blocks as your needs evolve.
- Flexible traffic management: Nginx server blocks offer precise control over website traffic, ensuring efficient routing and load balancing.
- Simplified configuration: Manage multiple websites with ease using the straightforward configuration structure of Nginx server blocks.
Prerequisites to Set Up Nginx Server Blocks on Ubuntu 20.04
Before continuing, make sure you've met the following requirements:
- A domain name that points to the IP address of your public server.
- Nginx installed on your Ubuntu computer.
- You are logged in as root or as a user with sudo access.
The term "Server Blocks" is also referred to as a "Virtual host" in some articles. A virtual host is a word used by Apache.
Creating the Directory Structure
The document root is the location where a domain name's website files are stored and provided in response to queries. You can choose any location for the document root. We'll use the following directory structure in this example:
/var/www/
├── domain1.com
│ └── public_html
├── domain2.com
│ └── public_html
The document root for each domain hosted on the server will be /var/www/<domain_name>/public_html
.
Begin by creating the domain's root directory:
sudo mkdir -p /var/www/domain1.com/public_html
In the domain document root directory, we'll also build an index.html
page that will be displayed when you visit the domain in your browser:
sudo nano /var/www/domain1.com/public_html/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Welcome to example.com</title>
</head>
<body>
<h1>Success! example.com home page!</h1>
</body>
</html>
The newly generated files and folders are owned by root because the instructions above were run as a sudo user. Change the ownership of the domain document root directory and all files within it to the Nginx user (www-data) to avoid any permission issues:
sudo chown -R www-data: /var/www/domain1.com
Creating a Server Block
Nginx server block configuration files are found in the /etc/nginx/sites-available
directory on Ubuntu systems. They can be enabled by making symbolic links to the /etc/nginx/sites-enabled
directory, which Nginx will read when it boots up.
Create the following server block file in your text editor:
sudo nano /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/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}
server_name
: The server block configuration domains that should match.root
: Nginx will serve the domain files from the root directory.access_log
,error_log
: Specifies the location for log files.
The configuration file can be titled whatever you like, but the domain name is usually the best choice.
Create a symbolic link from the file to the sites-enabled directory, which Nginx reads on startup, to enable the new server block file:
sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/
Unlink the default Nginx page.
sudo unlink /etc/nginx/sites-enabled/default
Check the syntax of the Nginx configuration:
sudo nginx -t
The output will look like this if there are no errors:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
To make the modifications take effect, restart the Nginx service:
sudo systemctl restart nginx
Finally, to ensure that the server block is functioning properly, access http://domain1.com
or http://_ip_
in your preferred browser and look for something similar to this:
FAQs to Set Up Nginx Server Blocks on Ubuntu 20.04
Where are the Nginx server block configuration files located?
The Nginx server block configuration files are typically located in the /etc/nginx/sites-available/
directory.
How do I create a new server block configuration file?
Use the command sudo nano /etc/nginx/sites-available/example.com
to create a new server block configuration file for the example.com
domain.
How do I enable a server block?
Run the command sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
to enable the server block.
How do I disable a server block?
Use the command sudo rm /etc/nginx/sites-enabled/example.com
to disable the server block.
How do I test the Nginx configuration for errors?
Run the command sudo nginx -t
to check the Nginx configuration for any syntax errors.
How do I restart Nginx after making changes to the server block configuration?
Use the command sudo service nginx restart
to restart Nginx and apply the changes.
Conclusion
We've taught you how to set up Nginx server blocks and use a single Ubuntu server to host several domains. You can build additional server blocks for all of your domains by repeating the steps indicated above.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.