Language: EN

como-instalar-y-configurar-nginx-en-raspberry-pi

How to Install and Configure Nginx on Raspberry Pi

Nginx is a high-performance web server widely used that is ideal for hosting websites, web applications, and services on your Raspberry Pi.

Nginx (pronounced “engine-x”) is an open-source web server and reverse proxy that is known for its high performance, low resource consumption and ability to handle a large number of simultaneous connections.

One of the things that stands out in Nginx is its ease of use as a reverse proxy and load balancer, making it a very useful tool in high-demand environments.

Installing Nginx on Raspberry Pi

To install Nginx on your Raspberry Pi, we open a terminal and execute the following command,

sudo apt install nginx

We wait for it to install, and that’s it. So easy! When it’s ready, we can check that Nginx is running with,

sudo service nginx status

If at any point we need to stop, start, or restart Nginx, we can do so with these commands

sudo service nginx stop
sudo service nginx start
sudo service nginx restart

Configuring Nginx

The main configuration of Nginx is located in the /etc/nginx/ directory. The main configuration file is /etc/nginx/nginx.conf. By default, it has a snippet like the following.

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	... several configuration items that we omit... 
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}

Where you see that the last line loads all .conf files from the folder /etc/nginx/conf.d/ and all files from /etc/nginx/sites-available/.

To keep everything organized, you will generally perform the configuration of each site in separate files in the /etc/nginx/sites-available/ directory.

Example of Website Configuration

Let’s look at an example of configuring a basic website in Nginx. With this configuration, Nginx will serve your website on port 80 and handle incoming requests.

First, we create a configuration file in /etc/nginx/sites-available/ for your website, for example, my-site:

sudo nano /etc/nginx/sites-available/my-site

We add the site configuration, including the root path of the site and the port

server {
   listen 80;
   server_name my-domain.com www.my-domain.com;

   root /var/www/my-site;
   index index.html;

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

Now we create a symbolic link in /etc/nginx/sites-enabled/ to enable the site.

sudo ln -s /etc/nginx/sites-available/my-site /etc/nginx/sites-enabled/

We can verify that the configuration is correct with this command. If we have any errors in the configuration, it will show them in the console.

sudo nginx -t 

Finally, we create a directory to place the files of our website.

sudo mkdir /var/www/my-site
sudo nano /var/www/my-site/index.html

We put the HTML content into the index.html file. Finally, we restart Nginx to apply the configuration,

sudo service nginx restart

Example of Nginx as a Reverse Proxy

Let’s look at another example of how to use Nginx as a reverse proxy to redirect traffic to different services on your Raspberry Pi.

For example, let’s say we want to configure Nginx to redirect HTTP traffic to a Node.js web server running on another port.

Here is an example configuration in /etc/nginx/sites-available/:

server {
    listen 80;
    server_name my-domain.com;

    location / {
        proxy_pass http://127.0.0.1:3000;  # Redirects traffic to a Node.js server on port 3000
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Now, we simply do the same as before. We create a symbolic link in /etc/nginx/sites-enabled/ to enable the site.

sudo ln -s /etc/nginx/sites-available/my-site /etc/nginx/sites-enabled/

We verify that the configuration is correct,

sudo nginx -t 

Finally, we restart Nginx to apply the configuration,

sudo service nginx restart