Bỏ qua để đến nội dung
Cloudflare Tunnel for homelab boxes Publish apps on a box behind NAT through a Cloudflare Tunnel on your own Cloudflare account — token permissions, creating the tunnel, hostnames, what happens on deploy, upload limits, TLS and errors (cf_tunnel_scope, tunnel_required).

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:

PermissionWhy
Account → Cloudflare Tunnel: Editcreate the tunnel, set its hostnames, read its status
Zone → DNS: Editwrite the CNAME records of the hostnames
Zone → Zone: Readlist 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.

  1. Open the homelab box, Settings > Homelab & Cloudflare Tunnel.
  2. Pick the zone your app hostnames should live under (for example example.com).
  3. 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.

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.

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.

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.

CodeMeaningFix
403 cf_tunnel_scopethe Cloudflare token lacks Cloudflare Tunnel: Editadd the permission to the token (step 1), retry
409 tunnel_requiredthe action needs a tunnel (a hostname route, an upload to a homelab box)create the tunnel, or deploy from Git
409 cloudflare_requiredno Cloudflare connection yetconnect Cloudflare under Connect > DNS providers
400 zone_requiredseveral zones are connectedpass zone
400 zone_not_connectedthe zone or hostname is not in the tunnel’s Cloudflare connectionuse a hostname in that account
409 dns_conflictanother record already holds the hostnamedelete it under DNS & TLS or pick another name
409 agent_update_requiredthe agent is older than 0.2.145update the agent
409 server_offlinethe box is offlinestart it, retry

The connector’s own log is on the box: journalctl -u mz-cloudflared -f.

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).