Setting up Wireguard + Reverse Proxies
Running a VPS tunnel connection to my home server so that domain name connections work
Background
I used to simply run my servers and connect through them with a vpn allowing me to jump right into my homeserver. I usually used it with tailscale which was really convenient and connecting it with pihole (acting as a local dns server) allowed me to connect to my homeserver anywhere I go. However, it was really hard for me to connect to my homeserver if I am on a foreign device as tailscale only allows for me to login via the app using my google account.
The free version also did not have many features like account sharing. So I'm stuck with all my services in one account. That's why I decided to explore more options.
Here's a diagram of what I am trying to set up:

Setting up Wireguard on a VPS
I decided to rent a cheap vps from Ionode which was around $2/month. After setting up the connection I installed wireguard on the VPS and my homeserver. For simplicity and allowing my wireguard to connect to various networks, I installed both without docker.
On Both:
Generate the public and private key on both VPS and Homeserver:
Save the public keys you received on both servers. Then setup /etc/wireguard/wg0.conf
Now we need to setup ipv4 forwarding to allow for traffic forwarding on the VPS. Open /etc/systl.conf and add:
Save the file and apply changes using:
Now start the wireguard services on both sides:
Check the status of the wireguard service using and pinging:
After making sure the connection is working its now time to setup the reverse proxy. Its also okay to do this using IPTables and directly forwarding the from 80 and 443 ports to wg0 network and through to homeserver but I feel more safe and comfortable with a reverse proxy sitting and parsing logs. It also allows for other additional setups like fail2ban and crowdsec to be added later!
Setting up Reverse Proxy
For this, I went with Nginx but Apache is fine too. First, install nginx on your VPS:
Next, open the configuration file:
Here, you will put a simple html file at the end. We do this to first get SSL certs first before pointing at our homeserver.
Note: This only applies if you make a new file. Link the site-available new file with sites-enabled default so that any changes is applied to both
Now, you point your bought domain name to the VPS ip address. Make sure you point your domain and all subdomains too by using the wild card *.yourdomain.com so that it forwards all subdomains related to this VPS. Note that in your config file, we also have a similar setup with all *.yourdomain.com pointing to the html file (for now) in the nginx file.
Restart nginx to apply changes and try accessing your site:
Now we use LetsEncrypt to get wildcard certs for your domain. Install LetsEncrypt:
Next, install the dns provider certbot (check your own dns provider first), here's a few examples:
After this, you will need your dns provider token (Digital Ocean, Cloudflare), create a file at /etc/letsencrypt/cloudflare.ini (digitalocean.ini, or whatever name you want) and add the token:
Tighten the permissions for the file:
Then request ssl certs:
Once complete, modify your /etc/nginx/sites-available/default file:
Now, restart nginx and try accessing your site to ensure that it's loaded in https. After it's working its time to point it to your homeserver.
After setting up your homeserver, its time to rewrite the VPS nginx configuration so that it points to the wireguard network:
Now it's time to change the Iptables so that traffic from the wireguard tunnel from in your homeserver is passing through the network you want. I prefer a reverse proxy also sitting behind accepting the traffic so I will reroute it to pass through 80 and 443
Setup your reverse proxy, use the any configuration but I recommend you test with a sample html file first. Also, instead of using LetsEncrypt to find a new SSL certificate, use the same one from your VPS.
If your reverse proxy is setup on the bare metal (the machine itself) use eth0 as your network id. If you reverse proxy is on a docker container network, check your docker container network id using the commmand: ip a
Now finally, you need to make sure the iptables are persistent so that it would be preserved even after a reboot. Install:
Then run this to save the current iptables:
You can also save your current iptables to a file for backups:
Now you're done! You have connection that is running from your VPS reverse proxy, through wireguard, to your home reverse proxy!
Last updated