Let’s Set Up a Reverse Proxy

Written by Carlos Oulman

March 13, 2022

So what the heck is a reverse proxy and why would you even want one? That’s a fair question and if you don’t understand how a reverse proxy works, the concept can be a little confusing at first. In some earlier tutorials, we set up a Linux system, installed Docker, and got Portainer up and running. This gives us some good groundwork for maximizing what we can run on our server but to understand why we would want a reverse proxy in the first place, we need to look at port numbers and understand them a bit.

Port numbers help computers identify specific processes related to network traffic. Some common ports you may have heard of are 80 (HTTP) and 443 (HTTPS). Without port numbers, network traffic would get lost. Normally, only one service can run on a single port. So if we have a web server set up on our server, we wouldn’t be able to have anything else on the same port. But what if we wanted to host multiple websites independently from one another on the same server? Well, that’s where a reverse proxy comes in. Instead of sending the request for our website to port 80 or 443, we set the reserve proxy to listen on those ports. When someone wants to connect to our website, the reverse proxy looks at where the user wants to go then forwards them to what they are requesting. It’s sort of like a router but just for port numbers. What’s great about this is that internally, you could run two web servers independently of each other, and using the reverse proxy, everything will just work.

There are different reverse proxies that you can use but the one we will be taking a look at today is Nginx Proxy Manager. If you haven’t set up Docker yet, you’ll want to do that first before following the setup instructions from the official documentation. When you have your container up and running, log in with the default credentials. Run through the initial setup and we’ll be ready to get started.

In this example, we’ll be configuring a webserver to run through our reverse proxy. This tutorial is assuming you already have a docker container setup for your webserver and it’s on the same network as your proxy manager. First, we need to make sure we have a DNS record created for our FQDN and point that to our server running Nginx Proxy Manager. We won’t be going through the processes as it’s different for every DNS manager. In NPM, we can click on Hosts then Add Proxy Host. Start by entering the domain name of your website. Since our webserver uses plain HTTP, we can leave the Scheme as it’s set. We need to enter the hostname or IP address of our webserver. If you’re using a Docker container, we can just enter the container name. Last, enter the port number of the container. This is a good opportunity to mention that our web server container should be set to another port besides 80 or 443.

One feature that’s great about NPM is that it allows us to easily request SSL certificates. Let’s generate one for our website. Click on the SSL tab then in the dropdown click Request a new SSL Certificate. Follow the steps and you should be all set. Click Save. You will see a new item in your proxy hosts for your webserver. Let’s test it by going to your website. If the reverse proxy is working as intended, you should see your website. Make sure to check out the official documentation to learn more about Nginx Proxy Manager.