configurar-nginx-dos-dominios-raspberry-pi

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

  • 4 min

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 option 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 must ensure our Raspberry Pi is up to date,

sudo apt update sudo apt upgrade

Also, we assume Nginx is installed and working correctly,

sudo apt install nginx

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

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

Create the Virtual Host Files

We are going to serve two different web pages, for two domains. For this example, we will assume they are named,

  • Dominio1.com
  • Dominio2.com

Of course, in your case, you will need to replace domain1.com and domain2.com with the domains you have purchased.

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

To do that, first create directories for the website files

sudo mkdir -p /var/www/dominio1.com/html sudo mkdir -p /var/www/dominio2.com/html

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

sudo chown -R www-data/var/www/dominio1.com/html sudo chown -R www-data/var/www/dominio2.com/html

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

Finally, create some test files for each domain.

echo ‘

Welcome to dominio1.com

’ | sudo tee /var/www/dominio1.com/html/index.html echo ‘

Welcome to dominio2.com

’ | sudo tee /var/www/dominio2.com/html/index.html

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

Configure Server Blocks in Nginx

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

When resolving requests,

  1. Nginx uses the request data to obtain the address being requested.
  2. It checks the defined server blocks.
  3. 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/dominio1.com

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;
	}
}
Copied!

Configure the second domain:

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

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;
	}
}
Copied!

That is, the same file. We’ve simply put domain1 in one and domain2 in the other.

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

Enable the Sites and Restart Nginx

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

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

Finally, restart Nginx to apply the new configuration.

sudo systemctl restart nginx

The way of working with symbolic links in Nginx is a convention. We could use text files directly. But the 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 configured earlier for each domain (which show welcome to Dominio1 or Dominio2, respectively).

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