Language: EN

configurar-nginx-dos-dominios-raspberry-pi

How to Configure Nginx to Serve Two Different Domains on Raspberry Pi

Nginx is a high-performance web server used to serve static and dynamic content on the web.

On a Raspberry Pi, Nginx can be an excellent choice for handling multiple domains and web applications from a single server.

In this tutorial, we will learn how to configure Nginx to serve two different domains on our Raspberry Pi.

Prerequisites

First, we need to ensure that our Raspberry Pi is up to date,

sudo apt update
sudo apt upgrade

Additionally, we understand that we have Nginx installed and running properly,

sudo apt install nginx

You should also have two domains pointed to the public IP address of your Raspberry Pi. For this, you should have the DNS records for each domain correctly configured.

This may vary depending on the domain provider, but you will typically find options to manage the DNS records in your domain’s management panel.

Create the virtual host files

We are going to serve two different web pages for two domains. For this example, let’s assume they are called,

  • Domain1.com
  • Domain2.com

Logically, in your case, you will need to replace domain1.com and domain2.com with the domains you have registered.

Before configuring Nginx, let’s create a few sample files that we will serve from each domain.

For that, first, we create directories for the website files

sudo mkdir -p /var/www/domain1.com/html
sudo mkdir -p /var/www/domain2.com/html

Additionally, we assign appropriate permissions to the directories for the www-data user.

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

www-data is the default user created by Nginx. In some versions, this user is nginx.

Finally, we create some test files for each domain.

echo '<html><body><h1>Welcome to domain1.com</h1></body></html>' | sudo tee /var/www/domain1.com/html/index.html
echo '<html><body><h1>Welcome to domain2.com</h1></body></html>' | sudo tee /var/www/domain2.com/html/index.html

We now have enough to run tests (in your project, you can put the real websites you want to serve)

Configure the server blocks in Nginx

Nginx uses configuration files to define how to handle each domain or applications. These files are called server blocks.

When resolving requests,

  • Nginx uses the request data to obtain the requested address
  • It checks the defined server blocks
  • If one matches the request, it executes that server block

So let’s create a configuration file for each domain in the Nginx configuration directory.

Configure the first domain:

sudo nano /etc/nginx/sites-available/domain1.com

We add the following configuration:

server {
    listen 80;
    server_name domain1.com www.domain1.com;

    root /var/www/domain1.com/html;
    index index.html;

    location / {
   	 try_files $uri $uri/ =404;
    }
}

Configure the second domain:

sudo nano /etc/nginx/sites-available/domain2.com

We add the following configuration:

server {
    listen 80;
    server_name domain2.com www.domain2.com;

    root /var/www/domain2.com/html;
    index index.html;

    location / {
   	 try_files $uri $uri/ =404;
    }
}

That is, the same file. Simply in one we have put domain1 and in the other domain2.

In both cases, we are associating a domain with a folder from which it will serve the files (that we created in the previous section)

Enable the sites and restart Nginx

Now, we create symbolic links to activate the configuration files in the sites-enabled directory.

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

Finally, we restart Nginx to apply the new configuration.

sudo systemctl restart nginx

The way to work with symbolic links in Nginx is a convention. We could use text files directly. But links simplify management and maintenance.

Verify the configuration

Now we need to verify that we can access the domains correctly. To do this, open your browser and visit http://domain1.com and http://domain2.com.

You should see the welcome pages we previously configured for each domain (which display welcome to Domain1 or Domain2, respectively)

Now you can modify the served files for each domain or apply specific configurations for each virtual server.