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:
- No open ports. The server only makes outgoing connections, so it works behind a home router and needs no firewall changes.
- A hidden address. Your DNS records point at the tunnel, not at your server, so nobody learns the server's real address from your domain.
- Certificates handled for you. Cloudflare holds the certificate for your domain, so the padlock works without anything on your server.
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:
- Cloudflare must accept it on its nameservers. The tunnel's DNS records are created on Cloudflare, so the domain has to use Cloudflare's nameservers. A few country endings do not allow that; check Cloudflare's domain requirements before you buy. (In direct mode any DNS provider will do.)
- Use the domain itself, or a subdomain you fully control. The install uses exactly three names: the domain,
api.in front of it andmatrix.in front of it. The homeserver's sign-in service lives under a path of the third (matrix.example.com/auth/), so there is noauth.name to create. You can install on a subdomain (community.example.com, givingapi.community.example.comandmatrix.community.example.com), but Cloudflare's free certificate covers only one level of subdomain, so names two levels deep need a paid certificate option. Installing on the bare domain avoids this.
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.
- 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.
- 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.
- Cloudflare copies the DNS records it can find. Delete any record for
example.com,api.example.comormatrix.example.com(anA,AAAAorCNAMErecord 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 asMXrecords for your email. - 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. - 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.
Recommended: if you bought the domain at Cloudflare itself, steps 2 to 5 are already done for you.
The sign-in during the install
When you answer tunnel, the installer prints one link:
- Open the link in a browser on any computer (it does not have to be the server).
- 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.
- The installer creates a tunnel named
blackbranch-example.com(your domain afterblackbranch-). - It routes the three names to that tunnel. This creates three
CNAMErecords in your domain's DNS, each pointing at<tunnel id>.cfargotunnel.comand proxied through Cloudflare. - It writes the tunnel's settings (
config.yml) and keeps the tunnel's credentials file, both in/opt/blackbranch/cloudflared/. - 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:
- Sign in to the Cloudflare dashboard.
- Open My Profile โ API Tokens.
- Find the token called Cloudflare Tunnel API Token (on older accounts Argo Tunnel API Token) for your domain.
- 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.
Recommended: revoke the token right after the installer prints "Installed". If you ever need a new tunnel, the installer signs in again and makes one.
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.
- 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).
- Copy the token Cloudflare shows (the long text after
--tokenin its install command). Do not install anything Cloudflare suggests; the install has its owncloudflared. - Add three public hostnames to the tunnel:
example.com,api.example.comandmatrix.example.com, each with service type HTTP and URLcaddy:80. - Run the installer with
--tunnel-tokenand 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