Black Branch/Docs
All sections

Putting your domain on Cloudflare

This page is for you only if you choose the tunnel front door (the default). After it, your domain will be on Cloudflare, you will know what the installer's Cloudflare sign-in does, and you will know which leftover permission to remove afterwards. In direct mode, skip to Installing.

What Cloudflare does for your site after the install, including what it can see and how payment webhooks get past its bot protection, is on Cloudflare.

What the tunnel is, and why it is the default

Normally a website's server waits for visitors to connect to it, so its address is public and its ports 80 and 443 are open to the whole internet. A Cloudflare tunnel turns that around. A small program on your server, cloudflared, opens a connection out to Cloudflare and keeps it open. Visitors connect to Cloudflare at example.com, and Cloudflare passes each request back down that connection to your server.

That gives you three things:

The cost is that Cloudflare sits in the middle of every visit: the encrypted connection ends at Cloudflare, and Cloudflare forwards the request to your server. See What Cloudflare can see before you decide.

Which domains work

Any domain ending works (.com, .tv, .gg, .io, .cafe and so on), with two conditions:

Endings such as .app, .dev and .page are on browsers' HTTPS-only list. That is no problem here: the site is always served over HTTPS.

โš  Choose once. The domain becomes the homeserver's name, so every account is @name:example.com for good. Pick a domain you will keep renewing, check its renewal price as well as its first-year price, and turn on automatic renewal at the registrar. A domain that lapses takes your whole community's address with it.

Preview note: an install on a subdomain two levels deep has not been tested.

Adding your domain to Cloudflare

Do this a day ahead if you can: the change at your registrar can take a few hours to reach everywhere.

  1. Make a free account at cloudflare.com, and turn on two-factor sign-in for it (My Profile โ†’ Authentication). The installer's sign-in gives your server power over tunnels and DNS on this account, so protect the account well.
  2. In the dashboard, choose Add a domain (it may say Add a site), type your domain, and pick the Free plan. Cloudflare's guide: Add a site.
  3. Cloudflare copies the DNS records it can find. Delete any record for example.com, api.example.com or matrix.example.com (an A, AAAA or CNAME record for those exact names). The installer creates those three itself, and Cloudflare refuses to create a record where one already exists. Leave everything else alone, such as MX records for your email.
  4. Cloudflare shows two nameservers, such as ada.ns.cloudflare.com. At the company you bought the domain from (the registrar), replace the domain's nameservers with those two. Cloudflare's guide: Update your nameservers.
  5. Wait until the domain shows Active in the Cloudflare dashboard. Cloudflare also emails you when it is.

๐Ÿ–ผ Image: the Cloudflare dashboard's domain overview showing the status "Active" and the two assigned nameservers.

The sign-in during the install

When you answer tunnel, the installer prints one link:

  1. Open the link in a browser on any computer (it does not have to be the server).
  2. Sign in to Cloudflare, pick your domain from the list, and confirm. You do not copy anything back; the installer notices by itself that the sign-in finished.
  3. The installer creates a tunnel named blackbranch-example.com (your domain after blackbranch-).
  4. It routes the three names to that tunnel. This creates three CNAME records in your domain's DNS, each pointing at <tunnel id>.cfargotunnel.com and proxied through Cloudflare.
  5. It writes the tunnel's settings (config.yml) and keeps the tunnel's credentials file, both in /opt/blackbranch/cloudflared/.
  6. It deletes the account certificate the sign-in left behind (see below), and later starts cloudflared.

๐Ÿ–ผ Image: the Cloudflare page that opens from the installer's link, with the list of domains to authorize.

Afterwards the dashboard shows the tunnel as healthy under Zero Trust โ†’ Networks โ†’ Tunnels, and the three records on your domain's DNS page.

If a step fails

Running the installer again carries on where it stopped. It never makes a tunnel twice.

What it says What happened What to do
"routing api.example.com to the tunnel failed" and "already has a DNS record" That name still has an A or CNAME record, often the bare domain's Delete that record on Cloudflare's DNS page, then run the installer again.
"the tunnel blackbranch-example.com exists in Cloudflare, but its credentials are not on this server" A tunnel with that name was made earlier, from another server or a wiped one Moving your site here from another server? Run the installer again with --moving (see Moving to a new server). A tunnel left from an install you gave up on: delete it in Zero Trust โ†’ Networks โ†’ Tunnels, then run the installer again.
"the Cloudflare sign-in did not finish" The link was not opened, or the sign-in was cancelled Run the installer again; it prints a fresh link.

The account certificate, and why you should revoke it

The sign-in gives cloudflared two different files. Cloudflare explains both in Tunnel permissions:

File What its holder can do What the install does with it
The account certificate, cert.pem Create, route, list and delete every tunnel on your Cloudflare account, and add DNS records for the domain you picked. It holds an API token that stays valid until you revoke it. Used once, for steps 3 and 4 above, then deleted from the server. It is never in a backup.
The tunnel credentials, <tunnel id>.json Run this one tunnel, and nothing else Kept in /opt/blackbranch/cloudflared/; cloudflared uses it every time it connects. It is in your backups.

Deleting the file from the server does not delete the API token inside it: the token stays valid at Cloudflare until you delete it. Nothing on your install needs it again, so remove it once the install has finished:

  1. Sign in to the Cloudflare dashboard.
  2. Open My Profile โ†’ API Tokens.
  3. Find the token called Cloudflare Tunnel API Token (on older accounts Argo Tunnel API Token) for your domain.
  4. Choose the three dots beside it, then Delete.

The running tunnel keeps working, because it runs on its own credentials file. Cloudflare describes this in Revoke account certificate.

Using a tunnel you made in the dashboard instead

You can create the tunnel yourself in Cloudflare's dashboard and give the installer only its token. The server then never holds an account certificate: the token can run that one tunnel and nothing else.

  1. In the dashboard, open Zero Trust โ†’ Networks โ†’ Tunnels โ†’ Create a tunnel, choose Cloudflared, and give it any name. Cloudflare's guide: Create a tunnel (dashboard).
  2. Copy the token Cloudflare shows (the long text after --token in its install command). Do not install anything Cloudflare suggests; the install has its own cloudflared.
  3. Add three public hostnames to the tunnel: example.com, api.example.com and matrix.example.com, each with service type HTTP and URL caddy:80.
  4. Run the installer with --tunnel-token and nothing after it. It asks for the token and does not show what you type. Or save the token in a file only you can read and pass --tunnel-token-file <file>.

Never put the token itself on the command line: every account on the server can see a running command's arguments, and sudo writes them to its log. The installer refuses a token given that way. The token is stored in /opt/blackbranch/.env as CLOUDFLARE_TUNNEL_TOKEN. To replace it later, run the installer again with --tunnel-token.

With a dashboard tunnel, the routes live in Cloudflare's dashboard, not on your server, so you change them there.

Behind the scenes

Inside the install, cloudflared hands every request to the router (Caddy) on the install's private network. Caddy trusts the visitor's real address, which Cloudflare sends in the CF-Connecting-IP header, only when the request comes from cloudflared's own fixed address on that network. That is what lets per-visitor limits, such as chat rate limits, count each visitor separately.

Next: Installing

โ† Install