Servers · 6 min read
Publishing a site with Cloudflare Tunnel
This website lives on a server at home, on a private IP. Cloudflare Tunnel is how it reaches the internet without opening any ports on my router.
Why not just port-forward?
- Port forwarding exposes your home IP and an open port to every scanner on the internet.
- Many ISPs use CGNAT, so port forwarding doesn't even work.
- You'd have to manage TLS certificates yourself.
How it works
A small program, cloudflared, runs on the server and opens an outbound connection to Cloudflare. When someone visits the domain, Cloudflare sends the request back down that connection to the local web server. Nothing inbound is ever opened.
Visitor → Cloudflare edge (TLS) → tunnel → cloudflared →
localhost:8080Setup (config-file method)
- Install
cloudflaredfrom Cloudflare's official package repository. - Log in and pick the domain:
cloudflared tunnel login - Create the tunnel:
cloudflared tunnel create portfolio - Point the domain at it:
cloudflared tunnel route dns portfolio example.com - Write the config file shown below, then run it as a service.
# /etc/cloudflared/config.yml
tunnel: <TUNNEL-ID>
credentials-file: /etc/cloudflared/<TUNNEL-ID>.json
ingress:
- hostname: example.com
service: http://localhost:8080
- service: http_status:404 # catch-all: anything else gets 404
You can also create the tunnel from the Cloudflare Zero Trust dashboard. It gives you a single install command with a token, and you manage the routes in the browser instead of a file.
Security checklist
- Point each hostname at one service and port, never a whole subnet.
- Keep the catch-all
http_status:404rule last. - The credentials JSON (or token) is a secret: never commit it to Git.
- Put private services (dashboards, SSH) behind Cloudflare Access login.
- Bind the web server to
127.0.0.1so it's only reachable through the tunnel.
Anything the tunnel can reach, the internet can reach. Keep the ingress list short.
← Back to all notes