Setup a reverse proxy using nginx with SSL

Reverse proxy are really useful for accessing services on your server via https instead of http. This is also required when using an api, for example, on your website that uses SSL. As it is not allowed to access http resources from an https connection.

In this post, I'll show you how to setup a reverse proxy using nginx and secure it with a SSL certificate.


This tutorial presupposes, that you've installed nginx and setup server blocks. If you haven't, here's a guide: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-debian-10.

Setting up the server file

Firstly, we'll want to create a file name after the service your trying to reverse proxy, for example: reverse-proxy.conf. To do that, type: nano reverse-proxy.conf or vim reverse-proxy.conf if you know how to use vim.

Then insert this basic file:

server {
    server_name proxy.example.com;
    
    listen 80;
    
    location / {
        proxy_pass http://localhost:8080;
    }
}
Server flie for reverse proxy nginx

Now enter these command, to create a symbolic link for the file reverse-proxy.conf:

sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/
Command for creating symbolic link

Restart nginx:

sudo systemctl restart nginx
Command for restarting nginx


And voila, you've created a reverse proxy making localhost:8080 available at proxy.example.com.

Creating and installing the SSL certificate

To be able to access the reverse proxy via https we'll have to create a SSL certificate using certbot.

I've created a comprehensive tutorial on how to create a certificate using certbot:

💡
Choose "nginx" as "your_webserver"

Then:

💡
Select the hostname of your reverse proxy, ex. "proxy.example.com"

If I have helped you, consider signing up for free to get access to all my guides and my newsletter.

💪
Consider signing up for free to get access to all my guides and my newsletter