Running a Reverse Proxy Server with Localtunnel
Reverse proxying services like ngrok or serveo allow you to forward traffic from a public domain to a local development machine. While those services are very helpful for certain development activities, they can also be abused by mischievous scammers. Because of this many of the free options (like serveo) have shut down service due to the overwhelming amount of fraudulent activity leaving one provider in the space: Ngrok. Ngrok is a solid product, but if you desire to run and control your own reverse proxy you’ve come to the right place!
In this guide, we will discuss how to setup your own reverse proxy using an open-source reverse proxy called localtunnel, ubuntu, and nginx.
Prerequisites
- Provision a cloud virtual instance or local server with Ubuntu 18.04 LTS
Setup DNS Records
Configure two A records for your domain:
If you are using this on your primary domain:
- An
A record
pointing fromyourdomain.tld
to your server IP - An
A record
pointing from*.yourdomain.tld
to your server IP
If you are using this on a subdomain (double subdomaining):
- An
A record
pointing fromtunnel.yourdomain.tld
to your server IP - An
A record
pointing from*.tunnel.yourdomain.tld
to your server IP
Installing Nginx and Node
Connect to a terminal session on your server or instance.
To install Nginx, use following command:
sudo apt update
sudo apt install nginx nodejs npm
Setting up localtunnel
Navigate to the following directory and clone the localtunnel repo:
cd /var/www/html/
git clone https://github.com/localtunnel/servercd server/
npm installsudo npm install -g esm
There is currently a problem in localtunnel server that causes explicitly requested subdomains to fail to release on disconnection. Since you are running your server, you can make the following changes to the lib/ClientManager.js
file starting at line 36
.
NOTE: If you make the change below, anyone who requests a specific subdomain will be granted it, even if it is currently in use. Ensure that your team does not accidentally (or intentionally) steal someone else’s subdomain.
Setup Nginx
Configure nginx using the following files from the nginx localtunnel repo:
sudo systemctl enable nginxsudo systemctl start nginxsudo systemctl status nginx
In /etc/nginx/nginx.conf
comment out the line that says
include /etc/nginx/sites-enabled/*;
If you encounter this error: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
when starting nginx checkout this link for a description and fix of the issue.
Setup SSL Certs
Setup your site.conf
file using the commands below:
cd /etc/nginx/conf.dsudo curl -LJO https://raw.githubusercontent.com/localtunnel/nginx/master/site.conf
Follow the tutorial below to setup SSL certs for your subdomain. When your certs are issued you will need to update the ssl_certificate
and ssl_certificate_key
fields in the site.conf
file with the path of your newly generated certs (which will be given to you by the certbot cli).
Setup systemd
Install the proxy service with:
cd /etc/systemd/systemsudo curl -LJ0 https://gist.githubusercontent.com/alexbeattie42/d0255d07a7b9e18d6eb77ec88c946b91/raw/730b85f6b51e1f6e2e8fe2dbf299f1e5a44ee3b6/proxy.service > proxy.servicesudo systemctl daemon-reload
Below is what the proxy.service file looks like.
IMPORTANT: Change the --domain
flag value on line 11 to your own domain!
Enable and start the services
Reload and Enable services and check their statuses:
sudo systemctl enable nginxsudo systemctl start proxy
sudo systemctl restart nginxsudo systemctl status proxy
sudo systemctl status nginx
Connect to the proxy
Run the following command from your machine to connect to the proxy from your local machine:
# Install localtunnel
npm install -g localtunnel# Running localtunnel:
# --port = the port your local application is running on
# --subdomain (optional) = the subdomain you'd like to be issued
lt --host http://tunnel.domain.tld --subdomain=my-app --port=8000
You will be issued an https URL from executing the final command, which you can use to access your local application!
As ever, QuarkWorks is available to help with any software application project — web, mobile, and more! If you are interested in our services you can check out our website. We would love to answer any questions you have! Just reach out to us on our Twitter, Facebook, LinkedIn, or Instagram.