In this tutorial, we will show you how to set up a reverse proxy. For those of you who didn’t know, A Nginx HTTPS reverse proxy is an intermediary proxy service that takes a client request, passes it on to one or more servers, and subsequently delivers the server’s response back to the client. If you have multiple servers, a reverse proxy can help balance loads between servers and improve performance. As a reverse proxy provides a single point of contact for clients, it can centralize logging and reporting across multiple servers.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step by step to set up a reverse proxy.
Prerequisites
- A server running one of the following operating systems: Debian-based or RHEL-based.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Nginx.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Setup Nginx Reverse Proxy
Step 1. First, let’s start by ensuring your system is up-to-date.
sudo dnf update
Step 2. Installing Nginx.
By default, the Nginx package comes in the default RHEL-based repository. Now run the following command below to install Nginx to your Fedora system:
sudo dnf install nginx
After the installation is complete, start the Nginx service and enable it to start automatically on boot by running the following commands:
sudo systemctl start nginx sudo systemctl enable nginx
For additional resources on installing Nginx, read the post below:
Step 3. Configuring Nginx.
Open the Nginx configuration file in your preferred text editor, such as nano:
nano /etc/nginx/nginx.conf
Within the http
block, add the following lines of code to create a new server
block:
server { listen 80; server_name your-domain.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; } }
In this example, we are using your-domain.com
as the domain name and localhost:3000
as the address of the application running on a different port.
Save and close the file, then restart the Nginx service for the changes to take effect:
nginx -t sudo systemctl restart nginx
Step 4. Testing the Reverse Proxy.
Now that the Nginx reverse proxy is set up, we can test it by visiting the domain name in a web browser. Nginx should route the request to the application running on a different port.
Congratulations! You have successfully set up a reverse proxy. Thanks for using this tutorial to set up an Nginx reverse proxy on the Linux system. For additional help or useful information, we recommend you check the official Nginx website.