Cloudflare Tunnel for homelab boxes
Nội dung này hiện chưa có sẵn bằng ngôn ngữ của bạn.
A box at home has no inbound path from the Internet. A Cloudflare Tunnel fixes that
without opening a port: the cloudflared connector on the box dials out to Cloudflare, and
Cloudflare sends visitors of your hostnames down that connection to the box’s Nginx.
MZPanel creates the tunnel on your own Cloudflare account through the Cloudflare connection you already use for DNS. It never runs through MZPanel’s network, and MZPanel only creates a tunnel when you ask for one.
1. Give the Cloudflare token the Tunnel permission
Section titled “1. Give the Cloudflare token the Tunnel permission”The tunnel and its DNS records are made with the API token of your Cloudflare connection (Connect > DNS providers). It needs:
| Permission | Why |
|---|---|
| Account → Cloudflare Tunnel: Edit | create the tunnel, set its hostnames, read its status |
| Zone → DNS: Edit | write the CNAME records of the hostnames |
| Zone → Zone: Read | list your zones (already needed for DNS) |
In the Cloudflare dashboard: My Profile > API Tokens, edit the token (or create one from
the “Edit zone DNS” template and add the account permission), and include the account that
owns the zone under Account Resources. MZPanel does not need a new connection when you
edit the existing token: the token value stays the same. Without the Tunnel permission every
tunnel call answers 403 cf_tunnel_scope.
2. Create the tunnel
Section titled “2. Create the tunnel”- Open the homelab box, Settings > Homelab & Cloudflare Tunnel.
- Pick the zone your app hostnames should live under (for example
example.com). - Click Create tunnel. The agent must be online: the connector is installed live.
MZPanel creates a tunnel named mzpanel-<server id>-<server name>, installs cloudflared as
the systemd service mz-cloudflared.service (the connector token sits in a root-only file,
never on the command line) and shows Connected (4 links) once the connector reached
Cloudflare. The box needs agent 0.2.145 or newer.
3. Deploy
Section titled “3. Deploy”With a tunnel, a deploy without a domain answers https://<app>.<zone>:
{ "name": "grafana", "url": "https://grafana.example.com", "note": "Published through this server's Cloudflare Tunnel on example.com." }MZPanel adds the tunnel route and a proxied CNAME grafana.example.com → <tunnel-id>.cfargotunnel.com before the build starts. A deploy with your own domain in the
same Cloudflare account is routed through the tunnel too. An existing A/AAAA/CNAME record on
the name is never overwritten: the deploy note then says dns_conflict.
Uploads: apps_source_upload on a tunnel box returns an upload URL on the tunnel
(mzup-<server id>.<zone>). Cloudflare limits one request to 100 MB on the Free and Pro
plans, so keep the archive small (the curl already leaves out .git and node_modules) or
deploy from Git.
4. More hostnames
Section titled “4. More hostnames”Add any hostname the box serves (a Docker stack, a site, a second name for an app) in the same
card, or with servers_tunnel_route_add. Pick the origin protocol:
- HTTP (default) for Docker apps and sites. On a tunnel box MZPanel keeps their origin on plain HTTP: Cloudflare serves HTTPS to visitors, and the tunnel itself is encrypted.
- HTTPS for Docker stacks, whose Nginx vhost always redirects to HTTPS.
Removing a hostname deletes its tunnel route and the CNAME MZPanel created. Deleting an
app (app.delete) removes the routes MZPanel added for it, with their CNAMEs, once the delete
job succeeded. Removing the server deletes the whole tunnel at Cloudflare (its connections,
the tunnel, and the CNAMEs MZPanel created).
MZPanel deletes a DNS record only while it is still the record it created: same record, in the hostname’s own zone, a CNAME on that name pointing at this tunnel. If you re-pointed or replaced it, MZPanel leaves it alone.
How TLS works
Section titled “How TLS works”Visitors get Cloudflare’s certificate for your zone (Universal SSL covers <name>.<zone>, one
level deep). Between Cloudflare and the box the traffic runs inside the tunnel. On a tunnel
box the agent does not add an origin certificate or an HTTP→HTTPS redirect for names that
resolve to Cloudflare: behind an HTTP connector that redirect would loop.
Sites (WordPress, PHP) behind a tunnel are not tested yet. WordPress must be told that visitors use HTTPS (it sees HTTP at the origin); Docker apps are the supported path.
Errors
Section titled “Errors”| Code | Meaning | Fix |
|---|---|---|
403 cf_tunnel_scope | the Cloudflare token lacks Cloudflare Tunnel: Edit | add the permission to the token (step 1), retry |
409 tunnel_required | the action needs a tunnel (a hostname route, an upload to a homelab box) | create the tunnel, or deploy from Git |
409 cloudflare_required | no Cloudflare connection yet | connect Cloudflare under Connect > DNS providers |
400 zone_required | several zones are connected | pass zone |
400 zone_not_connected | the zone or hostname is not in the tunnel’s Cloudflare connection | use a hostname in that account |
409 dns_conflict | another record already holds the hostname | delete it under DNS & TLS or pick another name |
409 agent_update_required | the agent is older than 0.2.145 | update the agent |
409 server_offline | the box is offline | start it, retry |
The connector’s own log is on the box: journalctl -u mz-cloudflared -f.
Automate it
Section titled “Automate it”servers_tunnel_create {id, body: {zone: "example.com"}} → 202 {jobId, tunnel}servers_tunnel_get {id} → {homelab, tunnel: {status, cfStatus, routes, …}}servers_tunnel_route_add {id, body: {hostname, service?: "http"|"https"}}servers_tunnel_route_delete {id, hostname} (scope data:delete)servers_tunnel_delete {id} (scope data:delete)The connector job kinds tunnel.install and tunnel.uninstall are not available through
jobs_create / POST /v1/servers/{id}/jobs (they need the connector token MZPanel gets from
Cloudflare): use servers_tunnel_create and servers_tunnel_delete.
REST: GET|POST|DELETE /v1/servers/{id}/tunnel, POST /v1/servers/{id}/tunnel/routes,
DELETE /v1/servers/{id}/tunnel/routes/{hostname}. Poll servers_tunnel_get after a create
until status is active (failed carries the reason in error).