Serve From the Home
Recently I did a VPS1 migration because to renew my old plan would have been over $300/year. I originally got this plan because it was part of a really good sale. I could stay with my old VPS provider (Vultr) for somewhere around $6/month for a few CPUs and 1GB ram, or I could jump ship to this shiny new hosting company; Hostinger; and get 8 CPUs and 16GB of ram, for ~10/month. I could use this new VPS to host my websites and game servers if I so desired!
But now that the sale is done the price went to the moon (~30$/month). Luckily, in the past 2 years since I last had to rent server space I have gotten much smarter (and my homelab has gotten much better)!
By the end of my run with the beefy VPS I was really only running a few websites (such as this), an email server, and a Matrix server. Matrix really doesn’t work well (tho I wish it did) so I was getting rid of that service anyway.
My homelab on the other hand, is now running rocking 2x 24 core Intel Xeons, 72GB RAM, 25TB Storage, and 4 Ethernet ports. If I tried to rent that kind of power in the cloud I would go broke nearly instantly. This system is very overkill for what I use it for even today but it’s fun to have the extra headroom (10yr old hardware is very cheap on Ebay).
Problem
So now that I have all this computing capacity at home, why would I need any in the cloud? Well, the public IP address.
Due to how the internet (networking in general) works, all computers have IP addresses. The most common being IPv4. The problem however is that there aren’t enough IPv4 addresses to go around. This makes them a scarce resource, which makes them cost money. For this reason your ISP2 doesn’t give residential connections (such as my homelab) a public IP.
What this means in practice is that I can’t route traffic inbound to my network, which is a basic requirement of hosting a website.
Solution
The two-part solution is first, a VPN3. My homelab lacks a public/routable IP, but my small VPS doesn’t.
But first, how do websites even work?
As you may know, I already use a reverse proxy for my websites. This is to maximize the amount of websites I can have on one server. To understand why this is useful we have to first understand how a browser gets to a web page.
- You type in a URL such as
https://oliveratkinson.netand hit enter - The browser asks a DNS server for the IP address of
oliveratkinson.net - The DNS server responds back with an IP
1.2.3.4 - The browser navigates to
1.2.3.4on port4434. - The server has a program listening on
1.2.3.4:443and responds to the request (in this case responds with HTML).
You may notice a problem.
How did the browser get the idea to send the request to port 443?
Nobody told it to go there, just a convention.
So what if I have multiple websites that I want to host?
I only have one public IP (1.2.3.4) and one port 443.
Does this mean I have to rent another IP just to host a second website?
Luckily, nope!
We can use a reverse proxy, such as Caddy.
What a reverse proxy does, is listen in the stead of the website application on port 443.
When it receives a request, it first looks at the metadata of the request (HTTP Headers) and sees what website it is destined for.
Once it learns this information, it can internally redirect this request to the website application, which has now been configured to listen on some other IP and port, such as localhost:8080.
In this example above, we are just redirecting the request along the VPS’s internal network (localhost). But nothing stops us from sending the request to a whole different server! This is where a VPN comes in handy.
If I can put my homelab and my VPS on the same network somehow, I can relay the requests from my VPS into my homelab.
Putting these two disparate devices on the same network is precisely what VPNs are for!
I can run a VPN serve on my VPS, then connect to it on my homelab server.
Each device now gets another IP such as 10.10.10.1 and 10.10.10.2 (IP address that start with “10” are private IP address, meaning the can’t be routed through the public internet, but I can create them without asking anybody).
Now instead of the reverse proxy relaying the incoming request to localhost:8080, it can relay it to 10.10.10.2:8080, which is in my homelab.
Now all the compute is happening in the homelab, decreasing the load on the public server5.
This was all to get IPv4 IPs working, but what about IPv6? Luckily, my ISP gives me a large (/56) block of publicly routable IPv6 addresses (4.7e21 addresses). The whole problem we’ve been describing up until now revolve around the fact that we ran out of IPv4 addresses. This is why IPv6 is so great! For just a standard residential plan my ISP gives me more publicly routable IPs than I could ever hope to use! This means that if the incoming connection is on IPv6, I can skip all that VPS/VPN trouble and just go straight to my home’s router, where it then gets routed directly to the machine it’s looking for! Nice and simple! The only downside is that bad ISPs (such as TDS, SHAME!) don’t give their customers IPv6 addresses, so not everybody can use this approach. Thus, we still have to bootstrap IPv4 into the situation with the VPS/VPN approach.
Another Possible IPv4 Solution
Besides running your own VPS you could always use something such as a Cloudflare tunnel to get the traffic from the public internet into your private machine. These work well in my experience but since I still need my VPS for my email server, thus the other way works well for me.
Footnotes
-
__V__irtual __P__rivate __S__erver; (part of) a rented computer stuck in a data-center somewhere. ↩
-
Internet Service Provider. The people who you pay every month to give you an internet connection. ↩
-
Virtual Private Network. A private network (like your home network, where all the devices can talk to each other), that doesn’t exist because of actual wires (virtual). ↩
-
Ports are a way to have multiple programs all listening for incoming messages on one computer. In this case, port
443becausehttpsmeans443,httpmeans80, Minecraft servers use25565, etc. You can view a whole list of these conventions here. ↩ -
This “load” is very minimal since I’m mostly running static websites, which would probably take less load to run directly on the VPS. However, for things such as my git server or game servers, there is actual load to offset by putting it on a different computer. ↩