How To Configure Nginx as a Reverse Proxy on Ubuntu 24.04

How to Configure Nginx as a Reverse Proxy on Ubuntu 24.04

Nginx or NGINX is a widely used web server with many features, including load balancing, reverse proxying, content caching, mail proxying, and more. It’s a flexible and robust solution known for its high performance and efficient use of resources. A reverse proxy manages incoming traffic and forwards requests to backend servers bound to specific TCP ports for applications like Node.js, Odoo, Tomcat, and others. In this tutorial, you will learn to configure Nginx as a reverse proxy on Ubuntu 24.04.

Prerequisites

• An Ubuntu 24.04 VPS.
• SSH root access or user with sudo privileges.

Step 1. Update System Packages

To start, log in to your Ubuntu 24.04 VPS using SSH:

# ssh root@IP_Address -p Port_number

Replace ‘IP_Address’ and ‘Port_number’ with your server’s actual IP address and SSH port number. If needed, replace ‘root’ with the username of your sudo account.

Once logged in, you must make sure that all Ubuntu packages installed on the server are up to date. You can do this by running the following command:

# apt update && apt-upgrade -y

Step 2. Install Nginx

If you don’t already have Nginx installed on your Ubuntu 24.04 server, you can install Nginx with the apt command:

# apt install nginx -y

Once you have installed Nginx, you can check the version of Nginx and if the Nginx server is running with the commands:

# nginx -version
nginx version: nginx/1.24.0 (Ubuntu)

The Nginx version is 1.24.0 at the time of writing the post.

# systemctl status nginx

If the Nginx server is up and running, you should receive similar output.

● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Wed 2024-10-16 07:27:58 UTC; 42min ago
Docs: man:nginx(8)
Process: 967 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 976 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 979 (nginx)
Tasks: 2 (limit: 4218)
Memory: 3.0M (peak: 3.2M)
CPU: 19ms
CGroup: /system.slice/nginx.service
├─979 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
└─980 "nginx: worker process"

Oct 16 07:27:58 systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server…
Oct 16 07:27:58 systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.

Now that you have Nginx installed and running, you can go ahead and set up a reverse proxy.

Step 3: Configure Nginx as a Reverse Proxy


Now that Nginx is installed, you can configure it as a reverse proxy. The main Nginx configuration files are stored in /etc/nginx/nginx.conf, while individual server configurations are in the /etc/nginx/sites-available/ directory.

First, you can create a new server block for your reverse proxy by navigating to the /etc/nginx/sites-available/ directory:

# cd /etc/nginx/sites-available/

and create a new configuration file, for example, reverse-proxy.conf:

# nano reverse-proxy.conf

Add the reverse proxy configuration. Below is an example configuration for proxying traffic to a backend server running on port 3000:

nginx
server {
         listen 80;
        
        server_name yourdomain.com;
       
        location / {
               proxy_pass http://localhost:3000;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header X-Forwarded-Proto $scheme;
       }
}


proxy_pass: This directive defines the backend server where requests should be forwarded.
The proxy_set_header directives are essential for preserving client information, which is helpful for logging and backend processing.

Now, you can enable the configuration by creating a symbolic link in the sites-enabled directory:

# ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf

Test Nginx configuration to ensure that there are no syntax errors:

# nginx -t

If the test is successful, restart Nginx to apply the changes:

# systemctl restart nginx

Now, open a web browser and visit http://yourdomain.com. If everything is set up correctly, you should see the content served by your backend application.

Step 6: Configure SSL

With reverse proxy configured for your domain you can now secure it with SSL, you can obtain a free SSL certificate using Let’s Encrypt. Here’s a quick way to do that:

Install Certbot with the command:

# apt install certbot python3-certbot-nginx

Obtain and install the SSL certificate for your domain:

# certbot --nginx -d yourdomain.com


Certbot will automatically configure your Nginx for SSL and reload the service. You can verify SSL by visiting https://yourdomain.com.

Conclusion


You’ve successfully configured Nginx as a reverse proxy on Ubuntu 24.04. Whether you’re proxying requests to a single application or using Nginx for load balancing across multiple servers, it’s an essential tool in any system administrator’s toolkit. Of course, you don’t have to configure a reverse proxy on Nginx if you use one of our VPS Hosting services. In this case, you can simply ask our expert Linux admins to configure Nginx’s reverse proxy for you. They are available 24×7 and will take care of your request immediately.

Leave a Comment