Cloudflare Tunnel for a self-hosted app: setup, Docker and SSH
What a Cloudflare Tunnel is, quick tunnels and named tunnels, a tested Docker Compose setup with a tunnel token, SSH through Cloudflare Access, and the limits that come with sending every request through Cloudflare.
TL;DR
- A Cloudflare (opens in a new tab) Tunnel is an outbound-only connection from a small daemon on your server,
cloudflared, to Cloudflare, which sends the requests for your hostname down it, so the server needs no open inbound port and no public IP address. - A quick tunnel (
cloudflared tunnel --url http://localhost:8080) gives a randomtrycloudflare.comaddress without an account, for testing only: no uptime guarantee and at most 200 requests in flight. - For production, create a named tunnel in the Cloudflare dashboard, run
cloudflaredbeside your app with the tunnel's token (one more service in Docker (opens in a new tab) Compose), and route a hostname on your Cloudflare domain to the app's service. - SSH can use the same tunnel, with
cloudflaredon your computer and an Access policy in front; the price of all this is that every request goes through Cloudflare, under its limits and terms.
On this page
A Cloudflare Tunnel connects a server to Cloudflare from the inside. A small daemon on the server, cloudflared, opens outbound connections to Cloudflare's network; when someone visits your hostname, Cloudflare sends the request down those connections and cloudflared hands it to your app. The server needs no open inbound port and no public IP address, so a tunnel works for a computer at home behind a router, a connection behind carrier-grade NAT, and a VPS whose firewall refuses every incoming connection.
This article is part of our guide to deploying a web app to a VPS. It covers quick tunnels for testing, a named tunnel in Docker Compose for production, SSH through the tunnel, and the trade-offs, from Cloudflare's documentation as it stood on 2 December 2026.
What was tested, and how. On 2 December 2026 we ran cloudflared 2026.11.2 under Docker 29.9.3 and Compose 5.7.1: a quick tunnel to an nginx (opens in a new tab) container, which answered 200 over HTTPS through Cloudflare, and one to a container that prints the headers it receives. We installed cloudflared from Cloudflare's apt repository in an Ubuntu 24.04 container, and started the named-tunnel Compose file with a placeholder token, which cloudflared read from the environment and refused, as it should. We did not create a named tunnel, a DNS record or an Access application in a Cloudflare account: those steps follow Cloudflare's documentation.
What is a Cloudflare Tunnel
Cloudflare describes it as an "outbound-only, post-quantum encrypted connection" between your infrastructure and its network. Each running cloudflared keeps four long-lived connections to two Cloudflare data centers.
Visitor → https://app.example.com → Cloudflare (TLS, cache, WAF, DDoS protection)
→ the tunnel's connections → cloudflared on your server → http://app:80
Your server: no inbound port open; cloudflared connects out to Cloudflare on port 7844A tunnel has three parts:
- The tunnel itself, an object in your Cloudflare account with an ID. When you create it in the dashboard, its configuration (which hostname goes to which service) is stored at Cloudflare: a remotely managed tunnel, the kind this article uses. Cloudflare also documents locally managed tunnels, configured in a file on the server.
- The connector,
cloudflared, running on your server or on another machine in the same network, with the tunnel's token. - Published applications: a hostname on your domain mapped to a local service, such as
app.example.comtohttp://localhost:8080. Cloudflare creates the DNS record, a CNAME to<UUID>.cfargotunnel.com, and applies its CDN caching, WAF and DDoS protection before a request reaches the tunnel.
Browsers can use HTTP and HTTPS services directly. The other services the routing page lists, such as SSH, RDP, SMB and raw TCP, need cloudflared on the client too; SSH below shows how.
Why use a tunnel
- No inbound port. The server's firewall can refuse every incoming connection:
cloudflaredonly connects out, to port 7844 over TCP and UDP (HTTP/2 or QUIC), and Cloudflare's own advice is to block all ingress traffic. - No public address. A computer at home whose router you can't or won't forward ports on, or a connection without a public IPv4 address, can still serve a site.
- Nothing for Docker to open. Ports that Docker publishes skip ufw's rules. Behind a tunnel the app publishes no port at all, so there is nothing to skip.
- HTTPS without a proxy on the server. In our tests visitors reached Cloudflare over HTTPS while the app spoke plain HTTP on its Docker network; no Caddy (opens in a new tab), nginx or certificate on the server.
Cloudflare quick tunnels: a public URL in one command
Cloudflare Tunnels come in two kinds. A quick tunnel needs no Cloudflare account and no domain:
cloudflared tunnel --url http://localhost:8080cloudflared prints a random https://<words>.trycloudflare.com address that forwards to your local port until you stop it. Cloudflare's limits: no uptime guarantee, at most 200 requests in flight (more get 429), no Server-Sent Events, and a new hostname every time. It is meant for testing and development, such as showing someone a branch before you have preview environments; for production Cloudflare recommends a named tunnel.
In Docker, localhost inside the cloudflared container is the container itself, so point it at the app's service name on the Compose network. The file we tested:
services:
app:
image: nginx:alpine
tunnel:
image: cloudflare/cloudflared:2026.11.2
command: tunnel --no-autoupdate --url http://app:80
depends_on:
- appdocker compose up -d
docker compose logs tunnel | grep trycloudflare.comOur tunnel registered its first connection about five seconds after it started. cloudflared warns that the new address "may take some time to be reachable", and it did: lookups of the hostname failed for up to a minute before it resolved. Then nginx answered HTTP/2 200, with server: cloudflare in the response. Anyone who finds the address reaches your app, so run docker compose down when you are done.
Named tunnels: a stable hostname on your domain
A named tunnel needs a domain whose DNS is on your Cloudflare account, because its hostnames are records in that zone.
1. Create the tunnel
In the Cloudflare dashboard, go to Networking → Tunnels, select Create Tunnel and name it. Under Setup Environment, choose Docker: the dashboard shows a docker run command that ends in --token eyJ…. Copy only the token, the long eyJ… string; the Compose file below runs the connector. The tunnel shows as Healthy once a connector is connected.
Cloudflare's token reference puts it plainly: "Anyone with the token can run the tunnel." Keep it out of git (opens in a new tab). If it leaks, select Rotate token in the dashboard and restart every connector with the new token: connectors already running keep their connections until they restart, so after a compromise Cloudflare's guide also disconnects them through the API.
2. Run cloudflared with Docker Compose
Put the connector in the same Compose project as the app. This is the file we started, with nginx standing in for your app's image:
services:
app:
image: nginx:alpine
restart: unless-stopped
tunnel:
image: cloudflare/cloudflared:2026.11.2
command: tunnel --no-autoupdate run
environment:
TUNNEL_TOKEN: ${TUNNEL_TOKEN:?set TUNNEL_TOKEN in .env}
restart: unless-stopped
depends_on:
- appNext to it, a .env file that only the deploy user can read (chmod 600 .env):
TUNNEL_TOKEN=eyJ...the-token-from-the-dashboard...What each part does:
- No
ports:anywhere. Nothing listens on the server's network interfaces;cloudflaredreaches the app over the Compose network by its service name. TUNNEL_TOKENis the environment variable behind the--tokenflag, so the token lives in.env, not in the Compose file or the command. The${TUNNEL_TOKEN:?…}form makes Compose refuse to start without it; we checked, and it stops with "required variable TUNNEL_TOKEN is missing a value". To read the token from a file instead, there is--token-file(TUNNEL_TOKEN_FILE).--no-autoupdateturns offcloudflared's own updates. In a container you update by changing the image tag and runningdocker compose pull && docker compose up -d, so the version that runs is the one in the file.restart: unless-stoppedbrings both containers back after a reboot.
With our placeholder token, the connector logged Provided Tunnel token is not valid. and restarted in a loop, which shows it reads the variable; with the real token it connects, and the dashboard shows the tunnel as Healthy.
Without Docker, install cloudflared from Cloudflare's apt repository and run it as a service with the token. We ran the first four lines on Ubuntu 24.04; the last needs a real token:
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt-get update && sudo apt-get install cloudflared
sudo cloudflared service install eyJ...the-token-from-the-dashboard...3. Route a hostname to the app
Open the tunnel in Networking → Tunnels, and on Routes select Add route → Published application:
- Hostname: a subdomain and one of your domains, for example
apponexample.com. - Service URL: where
cloudflaredfinds the app. With the Compose file above that ishttp://app:80, the service name and the port the container listens on. Withcloudflaredinstalled on the server, it is a local address such ashttp://localhost:8080for an app published on127.0.0.1:8080.
Cloudflare creates the DNS record when you save. If the app serves HTTPS itself, use an https:// service URL and set Origin Server Name to the name on its certificate; Cloudflare's guide says to leave Disable TLS certificate verification off. Each further hostname is one more published application on the same tunnel, pointing to another service on the same network.
From any computer, curl -I https://app.example.com should then answer with server: cloudflare among the headers.
4. What your app sees
Every request reaches the app from cloudflared, not from the visitor. In our header test the app saw the cloudflared container's address as the client, and the visitor's address in the Cf-Connecting-Ip and X-Forwarded-For headers, with X-Forwarded-Proto: https. Tell your framework to trust those headers from the tunnel only, or every visitor looks like one client to your rate limits and logs.
5. More than one connector
A tunnel can have up to 25 replicas (100 connections): more cloudflared processes with the same token, on other machines. Cloudflare sends each request to the geographically closest replica; for failover between servers and health alerts, its documentation points to Cloudflare Load Balancing instead. A second replica on the same server covers a crashed connector, not a server that is down.
SSH over a Cloudflare Tunnel
The tunnel can carry SSH too, so port 22 can stay closed to the internet. Cloudflare documents four ways to do it: cloudflared on your computer, Access for Infrastructure with short-lived SSH certificates, your own SSH keys through Cloudflare's device client, and a terminal in the browser. The first is the shortest to set up.
On the server. Add a published application with a hostname such as ssh.example.com, the service type SSH and the URL localhost:22 (Cloudflare's steps). localhost is the machine cloudflared runs on, so this fits cloudflared installed on the server as a service (the apt package above); from a container, localhost is the container. One connector on the server can carry both the app, published on 127.0.0.1, and SSH.
In front of it, an Access policy. Cloudflare marks this step as recommended: add a self-hosted application in Cloudflare Access for ssh.example.com, with a policy that names who may connect. Without one, only the SSH server's own authentication decides who gets in. Access is part of Cloudflare Zero Trust.
On your computer. Install cloudflared and add this to ~/.ssh/config:
Host ssh.example.com
ProxyCommand /usr/local/bin/cloudflared access ssh --hostname %hOur apt install put cloudflared at /usr/local/bin/cloudflared, the path in Cloudflare's example; with Homebrew on macOS, brew --prefix cloudflared shows where it is. Then run ssh you@ssh.example.com: cloudflared opens a browser window for the Access login, and your SSH client then authenticates to the server as it always does. Keep the server's SSH hardened with keys and no passwords: Access is a gate in front of SSH, not a replacement for it.
For scripts and CI, where nobody can log in through a browser, Access has service tokens: a Client ID and a Client Secret, accepted by a policy whose action is Service Auth. cloudflared access ssh --help (version 2026.11.2) lists --service-token-id and --service-token-secret, also read from TUNNEL_SERVICE_TOKEN_ID and TUNNEL_SERVICE_TOKEN_SECRET, so a job can pass the token to the same ProxyCommand from its secrets.
Limits and trade-offs
- Every request goes through Cloudflare. It applies its caching, WAF and DDoS protection to your traffic, so it handles that traffic decrypted. When Cloudflare or your connector is down, the site is down, and by design there is no way around it.
- Request limits. Cloudflare's proxy accepts uploads up to 100 MB on the Free and Pro plans and 200 MB on Business (larger ones get
413), and answers524when your app takes more than 125 seconds to respond. Big uploads need chunking, and slow exports a background job. - Video and large files. Cloudflare's terms for its CDN say that, outside Enterprise plans, serving video and other large files through it requires the paid services made for them (such as Stream and Images), and that Cloudflare may limit or disable the CDN for a site that serves video, or a disproportionate share of pictures, audio or other large files, without them. Published applications go through that CDN, so read those terms before you put a media library or a file-sharing app behind a tunnel.
- Your DNS stays on Cloudflare. The hostnames are records in a Cloudflare zone; leaving later means changing how the site is served, not just a record.
- Non-web services need
cloudflaredon every client, as SSH does.
When a public server with Cloudflare's proxy is simpler
If the server has a public address and can open ports 80 and 443, a proxied DNS record (the orange cloud) puts Cloudflare in front of your web traffic without a connector: Cloudflare answers the name with its own anycast addresses and routes HTTP and HTTPS through its network to your server. The difference is the open ports. Anyone who learns the server's address can skip Cloudflare, unless the server accepts web traffic only from Cloudflare's addresses, and on a Docker host that filter has to sit where published ports can't skip it, as the Docker and ufw article above explains. A provider's firewall outside the server, as in the Hetzner walk-through, is the other place for it.
| Cloudflare Tunnel | Proxied DNS record | DNS only | |
|---|---|---|---|
| Inbound ports on the server | None | 80 and 443 | 80 and 443 |
| Needs a public address | No | Yes | Yes |
| Extra software on the server | cloudflared | None | None |
| DNS shows the server's address | No | No | Yes |
| Reachable around Cloudflare | No | Yes, unless filtered to Cloudflare's addresses | Cloudflare is not in the path |
| Cloudflare's upload and timeout limits | Apply | Apply | Don't apply |
| SSH | Through the tunnel, cloudflared on each client | Directly | Directly |
Pick the tunnel for a computer at home or any machine the internet can't reach, or when you want no open web port at all. Pick a proxied record when the server is public anyway and you would rather not depend on one more process, or when SSH must work without cloudflared on every computer. Pick DNS only when you don't want Cloudflare in the path; the server then needs its own HTTPS, as in the VPS guide's Caddy setup.
Doing this with OpsCrew
OpsCrew, the open-source tool we build, can serve an app through a Cloudflare Tunnel on a server you already have or a computer at home. The server connects out to Cloudflare and opens no web port, its firewall then allows only SSH, and GitHub Actions (opens in a new tab) deploys through a separate SSH hostname that Cloudflare Access protects with a service token only your repository's deploy secrets hold. It needs a domain on Cloudflare and Cloudflare Zero Trust turned on (free), and it shows the tunnel, its DNS records and every change on the server as plans you approve by their hash before anything is created.
Whichever way the site is served, the server still needs its upkeep: hardening an Ubuntu server covers it, and the complete guide to deploying an app to a VPS covers the rest of the setup.
Key facts
| Fact | Value | Source |
|---|---|---|
| Quick tunnel limits | At most 200 requests in flight (more get 429), no Server-Sent Events, no uptime guarantee, a new hostname each time | Cloudflare Tunnel, Quick Tunnels (opens in a new tab)Verified on |
| What cloudflared needs through the firewall | Outbound TCP and UDP port 7844 to Cloudflare (HTTP/2 or QUIC); no inbound port | Cloudflare Tunnel, Configuration (opens in a new tab)Verified on |
| Replicas per tunnel | 25 replicas (100 connections) | Cloudflare Tunnel, Configuration (opens in a new tab)Verified on |
| Largest upload through Cloudflare's proxy | 100 MB on Free and Pro, 200 MB on Business, up to 5 GB on EnterpriseLarger requests get 413. | Cloudflare, Error 413 (opens in a new tab)Verified on |
| Time an app has to answer through Cloudflare | 125 seconds, then error 524Configurable only on Enterprise. | Cloudflare, Connection limits (opens in a new tab)Verified on |
Frequently asked questions
Is Cloudflare Tunnel free?
Cloudflare's Tunnel documentation says it is available on all plans, Free included. Access, which you need to put a login in front of SSH, is part of Cloudflare Zero Trust: its onboarding asks you to choose a plan and enter payment details even for the Free plan, which Cloudflare says it does not charge.
Does a Cloudflare Tunnel hide my server's IP address?
Your hostname's DNS record points to the tunnel's cfargotunnel.com name, and Cloudflare answers proxied names with its own addresses, so DNS doesn't show the server's address. An address that is already known, from an old DNS record for example, stays known, but with no inbound port open, knowing it doesn't let anyone reach the app around Cloudflare.
Sources
- Cloudflare Tunnel (opens in a new tab)
- Create your first tunnel (opens in a new tab)
- Quick Tunnels (opens in a new tab)
- Tunnel configuration (replicas, firewall rules, run parameters) (opens in a new tab)
- Run parameters (opens in a new tab)
- Tunnel tokens (opens in a new tab)
- Routing (opens in a new tab)
- Cloudflare packages (cloudflared for Debian and Ubuntu) (opens in a new tab)
- Connect to SSH servers through Cloudflare Tunnel (opens in a new tab)
- SSH with client-side cloudflared (opens in a new tab)
- Service tokens (opens in a new tab)
- Get started with Cloudflare One (opens in a new tab)
- Proxy status (opens in a new tab)
- Connection limits (opens in a new tab)
- Error 413 (opens in a new tab)
- Service-specific terms, application services (opens in a new tab)
- cloudflared releases (opens in a new tab)
Related articles
- Deploying
Migrate a Heroku app and its Postgres database to a VPS
A tested migration from Heroku to your own server: the Procfile as Docker Compose services, config vars to a file, Scheduler to supercronic, Heroku Postgres restored with pg_restore, a DNS cutover and a way back.
- Deploying
Deploy a Replit app to a VPS
How to move a Replit app to a VPS you control: push it to GitHub, turn the .replit build and run commands into a Dockerfile, re-create the secrets, and move the production database with pg_dump.
- Hosting
Moving off Vercel, Heroku, Render or Railway to your own server
The self-hosted alternative to a hosting platform: what you take over, when to stay, what a server costs, the three ways to run one, and a tested migration path to DNS cutover.