Nginx is a high-performance web server widely used and 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 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 about Nginx is its ease of acting 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, open a terminal and run the following command:
sudo apt install nginx
Wait for it to install, that’s it. It’s that easy! When it’s ready, we can verify that Nginx is running with:
sudo service nginx status
If at any point we need to stop, start, or restart Nginx we can do it with these commands:
sudo service nginx stop sudo service nginx start sudo service nginx restart
Configuring Nginx
The main Nginx configuration is located in the /etc/nginx/ directory. The main configuration file is /etc/nginx/nginx.conf. By default, it has a tip 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 the .conf files from the /etc/nginx/conf.d/ folder and all files from /etc/nginx/sites-available/.
To keep everything organized, you will generally perform the configuration for each site in separate files in the /etc/nginx/sites-available/ directory.
Example Website Configuration
Let’s see an example of a basic website configuration in Nginx. With this configuration, Nginx will serve your website on port 80 and handle incoming requests.
First, create a configuration file in /etc/nginx/sites-available/ for your website, for example, my-site:
sudo nano /etc/nginx/sites-available/my-site
Add the site configuration, including the site root path 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
Now 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 there is any error in the configuration, it will show it in the console.
sudo nginx -t
Finally, create a directory to put our website files.
sudo mkdir /var/www/my-site sudo nano /var/www/my-site/index.html
Put the HTML content into the index.html file. Finally, restart Nginx to apply the configuration:
sudo service nginx restart
Example of Nginx as a Reverse Proxy
Let’s see another example of how to use Nginx as a reverse proxy to redirect traffic to different services on your Raspberry Pi.
For example, imagine 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. 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/
Verify the configuration is correct:
sudo nginx -t
Finally, restart Nginx to apply the configuration:
sudo service nginx restart

