How to set Nginx server blocks on Ubuntu 22.04

5 mar 2024 3 min di lettura
How to set Nginx server blocks on Ubuntu 22.04
Indice dei contenuti

Introduction

Nginx pronounced “x-engine” is a high-performance, open-source HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet. It can be used as a standalone web server, load balancer, content cache and reverse proxy for HTTP and non-HTTP servers.

A server block is an Nginx directive that defines settings for a specific domain, allowing you to run more than one website on a single server. For each website, you can set the site's document root (the directory that contains the website's files), create a separate security policy, use different SSL certificates, and much more.

This article describes how to configure Nginx server blocks on Ubuntu 22.04. You will learn how to set up multiple websites on a single server and customize each site's settings to suit your needs.

Prerequisites

Make sure you have met the following requirements before continuing:

  • The domain name points to your public server IP.
  • Nginx is installed on your Ubuntu system.
  • You are logged in as root or a user with sudo privileges.

It is worth noting that in some articles related to web servers, the term “Server Blocks” is referred to as “virtual host”. A virtual host is an Apache term.

Creating the directory structure

The document root is the directory where website files for a domain name are stored and served in response to requests. You can set the document root to any location you want. In this example we will use the following directory structure:

/var/www/├── domain1.com
 │   └── public_html
 ├── domain2.com
 │   └── public_html

Each domain hosted on the server will have the document root set to /var/www/<domain_name>/public_html.

Start by creating the root directory for the domain:

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

Next, create an index.html file and place it in the root directory of your domain. This file will be displayed as the default page when you access the domain URL in your web browser.

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

When you run commands as the sudo user, the newly created files and directories will be owned by the root user. To avoid permission issues, change ownership of the domain document root directory and all files within the directory to the Nginx user ( www-data ):

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

Creating a server block

On Ubuntu systems, the configuration files for Nginx server blocks are stored in the /etc/nginx/sites-available directory. They can be enabled by creating symbolic links to the /etc/nginx/sites-enabled directory, which Nginx reads during startup.

Open your text editor and create 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;
 }
  • server_name - The domains that will match this server blocking configuration.
  • root: The directory from which Nginx will serve the domain files.
  • access_log, error_log: Specifies the path to the log files.

The configuration file can have any name you like, but it is usually best to use the domain name.

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

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

To make sure your Nginx configuration syntax is correct, run a test:

sudo nginx -t

If there are no errors, the output will look like this:

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 for the changes to take effect:

sudo systemctl restart nginx

Finally, to verify that server blocking is working as expected, open the http://domain1.com browser of your choice and you'll see something like this:

Conclusion

We've shown you how to create Nginx server blocks and host multiple domains on a single Ubuntu server. You can repeat the steps above and create additional server blocks for all your domains.

If you encounter any problems, feel free to leave a comment.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.