IPv6 for your home lab web server - Cloudflare Tunnel and nginx
This site, ipv6.help, runs on a small server at home. The server doesn't even have a global IPv6 address, but the site is still reachable over both IPv4 and IPv6. The trick is Cloudflare Tunnel. The server calls out to Cloudflare and it's Cloudflare that answers the visitors, on IPv4 and IPv6. No open ports in the router, no port forwarding and it works behind CGNAT too. π
Here's how I do it, with nginx on an Ubuntu/Debian machine. Swap example.com for your own domain.
Does the domain have to be at Cloudflare?
Both yes and no. The domain can be registered anywhere, you don't have to move it. But on the free plan the DNS has to be at Cloudflare, meaning the name servers for example.com have to point to Cloudflare's. Just adding a CNAME at your usual DNS provider (partial setup) needs the Business plan, and delegating only a subdomain needs Enterprise.
If you don't want to move the DNS for your real domain, buy a cheap domain just for the lab. That's the easiest.
Just for testing you can also run a so-called Quick Tunnel without any domain at all, then you get a random address under trycloudflare.com. But it's meant for testing, not something to build on.
1. Add the domain to Cloudflare
Create a free account at cloudflare.com, choose Add a domain and type example.com. Cloudflare reads the DNS records that exist and gives you two name servers. Switch to them wherever you registered the domain. It takes from a few minutes up to a day before it goes through, Cloudflare sends an email when the domain is active.
2. Create the tunnel
In Cloudflare: Zero Trust β Networks β Tunnels β Create a tunnel. Choose Cloudflared, give the tunnel a name, for example homelab, and save.
Now an install box with a long token shows up. Choose Debian and run it on the server, or do it like this:
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt update && sudo apt install cloudflared
sudo cloudflared service install YOUR-TOKEN
That way cloudflared comes from Cloudflare's own package repository and is updated with apt like everything else. It starts as a systemd service:
systemctl status cloudflared
After a little while the tunnel shows as Healthy in Cloudflare.
3. Connect the domain to nginx
Under the tunnel, the Public Hostname tab (called Published application routes in newer versions of the dashboard), add:
- Subdomain: www (or leave empty for example.com)
- Domain: example.com
- Service: HTTP and localhost:80
Cloudflare creates a DNS record for www.example.com pointing to the tunnel by itself. It's proxied (orange cloud) and then Cloudflare answers on both A and AAAA. IPv6 is on by default for all domains at Cloudflare, it's under Network β IPv6 Compatibility if you want to check.
Cloudflare takes care of the certificate too, and the traffic between Cloudflare and the server goes encrypted in the tunnel. So nginx can run plain http on localhost.
4. nginx
Install it if it's not there already:
sudo apt install nginx
Put a site in /etc/nginx/sites-available/example.com:
server {
listen 127.0.0.1:80;
listen [::1]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
# The visitor's real address comes from Cloudflare in a header
set_real_ip_from 127.0.0.1;
set_real_ip_from ::1;
real_ip_header CF-Connecting-IP;
location / {
try_files $uri $uri/ =404;
}
}
Having nginx listen only on localhost means it isn't visible on the network at all, everything goes through the tunnel. Without the real_ip lines the log would just say 127.0.0.1 for every visitor, now their real IPv4 or IPv6 address is there.
Enable the site and reload:
sudo mkdir -p /var/www/example.com
echo 'Hello over IPv6!' | sudo tee /var/www/example.com/index.html
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
5. Test
From a computer with IPv6:
dig AAAA www.example.com +short
curl -6 https://www.example.com/
dig should answer with addresses starting with 2606:4700 or 2a06:98c0, those are Cloudflare's. And curl should show the page. Then look in the log, visitors over IPv6 show up with their IPv6 addresses:
sudo tail -f /var/log/nginx/access.log
If you don't have IPv6 at home you can test from your phone on the mobile network, most mobile operators have IPv6. Or check your own connection at test-ipv6.se.
Cloudflare's cache
Cloudflare caches some of what the server sends out, and it's good to know about before you start swearing about a change that doesn't show up. π
- By default static files are cached, meaning images, css, js, fonts, pdf and the like. The HTML pages themselves are not cached, they're fetched from your server every time.
- Cloudflare follows the Cache-Control that nginx sends. If you want to control it yourself you can add something like this to the server block:
location ~* \.(css|js|png|jpg|svg|woff2)$ {
expires 7d;
}
- If you change a css or js file and it doesn't show, the old one is still in the cache. Either you purge it under Caching β Configuration β Purge Cache, or you add a version number to the link, like
style.css?v=2. The query string is part of the cache key so a new one gets fetched. That's how this site does it. - If you're tinkering a lot there's Development Mode under Caching, it turns off the cache for three hours.
- If you want HTML cached too, for example for a site that rarely changes, you can do it with a Cache Rule. But start without, it's easy to fool yourself otherwise.
The cache works the same over IPv4 and IPv6, it's the same cache no matter how the visitor comes in.
Good to know
- Cloudflare sees all the traffic in clear text, since that's where TLS ends. For a home lab that's usually fine, but worth knowing.
- The server doesn't need any IPv6 at all for this, but that's no reason not to. π If you want to reach it with SSH or anything else from outside, real IPv6 at home is still best, see IPv6 at home and Getting started with IPv6 - route64.org/MikroTik if your ISP doesn't have it yet.
- More websites on the same server: add one more Public Hostname per domain and one server block per domain in nginx. The same tunnel is enough.