Cloudflare Tunnel
Take websites in through Cloudflare with no open ports on your servers.
With Cloudflare Tunnel, your servers open no ports for websites. A small connector in your cluster dials out to Cloudflare, and Cloudflare sends your visitors through it to Pyo Gateway. Pyo sets up the tunnel, its routes, the DNS records and the certificate for you.
It also lets Pyo run on a server where another program already uses ports 80 and 443, like Traefik or nginx.
What you need
- Your domains in a Cloudflare account.
- A Cloudflare token with these permissions:
| Type | Resource | Permission |
|---|---|---|
| Zone | Zone | Read |
| Zone | DNS | Edit |
| Zone | SSL and Certificates | Edit |
| Account | Cloudflare Tunnel | Edit |
- Your servers can reach Cloudflare on TCP port 7844.
After you add the permissions in Cloudflare, choose Test on the Cloudflare card under Integrations. Zones that can use the tunnel show in green. A zone that lacks one of the two tunnel permissions says which one. A zone with neither is DNS only.
Turn it on
- Open your cluster's Settings and find Traffic.
- Turn on Cloudflare Tunnel.
- Pick a default domain if you want one. Services get names under it, like
web.example.com. - Select Turn on.
Pyo creates the tunnel and starts two connectors, on different servers when it can. Your domains stay where they are. Move them one at a time, or all at once by closing public ports.
If the switch is greyed out, the cluster's agent can't run a tunnel yet, or its install is older than Pyo's. Run the command under Refresh this cluster in the cluster's settings, then try again.
Team owners and admins change a cluster's traffic.
It works the same on Pyo Cloud. Your Cloudflare token stays with Pyo, and your cluster only gets the tunnel's own token. On Pyo Cloud, run the command under Refresh this cluster once before you turn it on.
Close public ports
Turn off Public ports while the tunnel is on. Pyo moves the domains in your Cloudflare account to the tunnel first, and closes the ports once every one of them is on it. A domain waiting for Replace and switch holds the ports open until you decide. Domains outside your account stop answering from the internet.
Move one domain
On a service's Domains tab, select Switch to Tunnel. Pyo adds the tunnel's route first, then points the name at the tunnel with a proxied CNAME record. While the record moves, the domain answers both ways, through the tunnel and directly. Once Cloudflare sends visitors through the tunnel, Pyo Gateway only lets the tunnel in. A client that connects to your server directly gets a 403.
Paths of the domain on other services, like /api on an API service, move with it. Pyo points the name at the tunnel only once every one of them answers through it. Switch back takes them back the same way.
Hover the Cloudflare logo before the name to see the record it points at. To move it back, open the domain's menu and choose Switch back from Tunnel. The domain's own page has a Switch back button too.
If Cloudflare already has a record for the name, Pyo shows it and asks before it replaces it. Pyo keeps a copy, so Switch back puts it back. Developers can switch a domain when nothing is in the way. Replacing a record, or putting one back, is for team owners and admins.
Pyo edits the record at the name in place. It never deletes it first and then makes a new one, so DNS resolvers never get an empty answer to cache. When the name has several records, Pyo keeps one, removes the others, and then edits the kept one. Switch back edits Pyo's record back into yours, then makes your other records again.
When the old way closes
After the record moves, the old way keeps working until Cloudflare sends visitors through the tunnel. Pyo waits at least 90 seconds, or the old record's TTL plus 30 seconds when it wasn't proxied. Then it checks that a request to the name arrives through the tunnel, 3 times in a row, before it closes the old way.
The check asks for /.well-known/pyo-tunnel/… on the name, like a visitor would. Through the tunnel, the connector answers it itself, so it never reaches your service, its logs or its analytics. Until Cloudflare follows, the check goes the old way, to the origin the old record pointed at.
If Cloudflare still sends visitors the old way 15 minutes after the wait, the domain says so, and both ways stay open. Pyo keeps checking. Team owners and admins can select Finish now next to that line to close the old way at once. Visitors Cloudflare still sends the old way then get a 404. Use it too when the zone's security settings, like Bot Fight Mode or Under Attack mode, challenge the check.
pyo domain tunnel app.example.com --finishSwitch back
Switch back puts the record back first. The domain shows Switching back, and the tunnel keeps answering for the name until Cloudflare and DNS resolvers have the record: 90 seconds when the record put back is proxied, 5 and a half minutes when it isn't. After that, Pyo also waits until requests to the name no longer arrive through the tunnel.
The domain's own certificate comes back as soon as the record is back: the one you uploaded, or a new one from Let's Encrypt. A domain that had no HTTPS before keeps the tunnel's certificate until it's off the tunnel.
When you add a domain on a cluster with the tunnel on, you pick its path in the dialog and in the new service wizard. See Traffic path.
Tunnel domains
With a default domain, a cluster in tunnel mode gives each service a tunnel domain under it, like web-shop.example.com. Pyo makes one wildcard record, *.example.com, that points at the tunnel. Tunnel domains need no record of their own. A record you made for a name still comes first, so your existing sites aren't touched.
A name under the default domain that no service has gets a 404.
A name under the default domain always goes through the tunnel, so it has no Switch back. That goes for a tunnel domain and for a domain you add under it. Pyo refuses to move it back while the default domain covers it. The one exception is a name whose old record Pyo replaced. Switch back puts that record back, and it answers before the wildcard.
Pyo checks the wildcard record every 2 minutes. When Pyo made it and it still points at an old tunnel, Pyo edits it in place to point at the current one. A wildcard someone else made stays as it is. The Traffic panel and pyo cloudflare tunnel status then say so, and your other domains keep moving to the tunnel.
The default domain must be one of your zones itself, like example.com. Cloudflare's free certificate covers one level below a zone, so a name like web.apps.example.com wouldn't work.
Without a default domain, a cluster in tunnel mode has no tunnel domains. Only domains you switch go through the tunnel.
Move sites from Traefik with no downtime
- Connect the server to Pyo. Turn on Cloudflare Tunnel for the cluster.
- Deploy each site as a service in Pyo and add its domain.
- Select Switch to Tunnel on the domain. Traefik keeps serving until Cloudflare points at the tunnel.
- Check the site. If something's wrong, choose Switch back from Tunnel in the domain's menu.
- When every site has moved, stop Traefik.
Visitors
Pyo Gateway takes the visitor's address only from the connectors. Firewall rules, rate limits, the WAF, country rules, analytics and the audit log all see the real visitor. A service doesn't need Behind Cloudflare for its tunnel domains.
Your service gets the visitor's address in X-Forwarded-For, as the only address there. While a domain is still moving, a client that connects directly shows up with its own address, whatever header it sent.
Visitors who type http:// are sent to HTTPS, whatever your zone's Always Use HTTPS says.
TCP and UDP ports
Cloudflare Tunnel carries websites only. Public TCP and UDP ports stay on your servers' public addresses, or on Tailscale. They don't go through the tunnel. To close them too, turn off Publish TCP and UDP ports under Traffic while public ports are off.
Status
The Traffic panel shows how the tunnel is doing:
- Tunnel connected, with its connectors and Cloudflare's locations.
- Tunnel degraded, when a connector is down.
- Tunnel down, when no connector reaches Cloudflare. Its websites don't answer. Pyo sends an alert after 5 minutes.
A connector that can't start says why, like ImagePullBackOff.
Each domain on a service's Domains tab shows where it is: moving to the tunnel, on it, or Switching back. A domain that waits for Cloudflare's edge longer than 15 minutes says so there, with Finish now.
From the CLI:
pyo cloudflare tunnel status --cluster euCertificate and token
Pyo issues a Cloudflare Origin CA certificate for the tunnel's names. Only cloudflared sees it. Your visitors see Cloudflare's own certificate. It's valid for 15 years, and Pyo issues a new one when a name it doesn't cover goes through the tunnel. It's listed under Certificates as Cloudflare Tunnel.
The connector runs with the tunnel's own token, kept in your cluster. Pyo never sends your Cloudflare token to the cluster. Rotate gives the tunnel a new token and a new certificate.
Cloudflare's limits
- Uploads over 100 MB fail on the Free and Pro plans.
- Cloudflare waits 100 seconds for a response to start.
- WebSockets work. gRPC needs the zone's gRPC setting in Cloudflare.
Don't change Pyo's tunnel in Cloudflare. Pyo puts its routes back.
Turn it off
Turn off Cloudflare Tunnel under Traffic. Pyo lists the domains it touches first. With public ports on, each domain switches back: the records Pyo replaced come back, or Pyo points the name at your server. Then Pyo deletes the tunnel, its certificate and only the records it made. Names under the default domain stop answering. With public ports off, the tunnel's domains stop answering. Deleting a service or a cluster cleans up its tunnel records the same way.
From the CLI:
pyo cloudflare tunnel on --cluster eu --default-domain example.com
pyo domain tunnel blog.example.com
pyo domain tunnel blog.example.com --replace
pyo domain tunnel blog.example.com --finish
pyo domain tunnel blog.example.com --back
pyo cloudflare tunnel off --cluster eu
pyo cloudflare tunnel remove --cluster eu
pyo cloudflare tunnel rotate --cluster eu