How to Set up a Nginx Server Block on Ubuntu Linux
You set up an Nginx server block on Ubuntu Linux to manage multiple websites on a single server.
Nginx server blocks are core configuration units that guide the Nginx web server. A single Nginx server block manages incoming visitor requests for one specific website, defining its own request management rules. One Nginx server block can manage up to 10 distinct websites simultaneously.
Nginx on an Ubuntu machine can handle multiple websites. For instance, Nginx can show example.com on port 80 and another.org on port 443 using a unique SSL certificate for each. The Nginx server block file defines important settings for each website, including where website files are stored (document root), security rules, and the location of SSL certificates.
This method efficiently utilizes your server’s resources, enabling you to host multiple sites without requiring separate physical or virtual machines.
Create website directories in /var/www/, then create an Nginx configuration file in /etc/nginx/sites-available/. Link this file to /etc/nginx/sites-enabled/ and restart Nginx.
How to create website directory structures on Ubuntu Linux
Creating website directory structures on Ubuntu Linux means making a special folder for each site’s files, known as the document root, so your server can find them easily.
Here’s an example of a directory structure for multiple websites with unique content and domains.
Each website domain requires a distinct folder for its files, known as a document root. For instance, the website "example.com" stores its files within the `/var/www/example.com/public_html` folder. This document root configuration enables Nginx to deliver the correct website content for multiple sites on a single server, preventing content mix-ups for up to 10 unique domains.
Create a directory for the example.com website and its files using the command `sudo mkdir -p /var/www/example.com/html`. This command creates the necessary folders for website content.
sudo mkdir -p /var/www/example.com/public_htmlYou’ll need an index.html file in each document root to display content to visitors. Run the commands below to create an index.html file for the example.com domain.
sudo nano /var/www/example.com/public_html/index.html
Then copy and paste the content below into the file and save it. This is a basic HTML file we’ll use for testing.
<!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>sudo chown -R www-data: /var/www/example.com
How to create an Nginx server block file on Ubuntu Linux
Now that you’ve created domain content in the directory above, configure the Nginx server block configuration file for that content.
You’ll find the Nginx server block configuration files on Ubuntu Linux within the /etc/nginx/sites-available directory.
To create a server block file in the sites-available directory for our content, run the commands below. This creates a site-specific Virtual Host configuration file.
sudo nano /etc/nginx/sites-available/example.com.confAn example configuration that should work with most environments is shown below. Copy and paste the content below into the file you just created and save it.
You use the symbolic link command to enable the new server block configuration file. This creates a symbolic link from the server block file to the sites-enabled directory.
Run the commands below to enable the configuration file for our domain.
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
After that, run the commands below to restart the Nginx service.
sudo systemctl restart nginx
Once Nginx is restarted, browse to the server hostname or IP address. It should display the content file we created above.

Repeat this process for other domains and their server block configuration files. You can create as many virtual host files as your server can handle.
That should do it!
Conclusion:
- Configuring Nginx server blocks allows you to host multiple websites on a single server, optimizing resource usage.
- Each website requires a unique directory structure, document root, and configuration settings.
- By following the steps outlined, users can create and enable server blocks for different domains effectively.
- Always ensure proper permissions are set for document root directories to avoid access issues.
- Regularly check server logs to monitor traffic and troubleshoot any problems that may arise.
- Feel free to create additional server blocks as needed, expanding your web hosting capabilities.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!