After a second cloudflared replica joined a locally-managed Cloudflare Tunnel on another host, users in one region get 502s and 404s; why, and how do you fix it?
answer
- the edge picks a nearby connector
- localhost means this host
- each host reads its own file
- tunnel info lists connectors
basics
~20 sCloudflare sends each region's traffic to a nearby replica, and each replica resolves localhost and reads config.yml on its own host. Host B lacks the dashboard (502) and the new rule (catch-all 404). Use reachable service addresses and one identical configuration.
solid answer
~40 sA replica is another `cloudflared` running the same tunnel, and Cloudflare routes to an available connector, generally the geographically closest, with no guarantee which. So one bad replica shows up as one region's problem. Here host B resolves `service: http://localhost:8080` against itself, where nothing listens, so it returns a `502`. Its own `config.yml` also lacks last week's hostname, so those requests hit the `http_status:404` catch-all. Confirm with `cloudflared tunnel info <NAME>` (connector IDs, origin IPs, edge locations) and with `cloudflared tunnel ingress rule <URL>` on each host. Fix it with service addresses every replica can reach and one identical configuration, or move to remote management.
code
bash · 3 linescloudflared tunnel info admin-tunnel
cloudflared tunnel --config /etc/cloudflared/config.yml ingress rule https://reports.example.com/
cloudflared tunnel cleanup --connector-id <CONNECTOR_ID> admin-tunnelgo deeper
Recall that a replica is another cloudflared running the same tunnel, that each adds four connections, and that a tunnel allows up to 25 replicas.
Explain that the edge picks a nearby connector with no guarantee, and that each replica resolves service addresses and reads its configuration on its own host.
Diagnose by connector with tunnel info and per-host ingress rule checks, then fix drift with reachable addresses, identical configuration and a rolling replica change.
Decide when replicas suffice and when separate tunnels behind a load balancer are needed for steering, failover rules and session affinity.
## What a replica is A **replica** is one more `cloudflared` process running the **same tunnel**, usually on another host. A locally-managed tunnel's replica uses the same credentials file; a remotely-managed tunnel's replica uses the same token. Each replica opens its own **four connections**, and a tunnel can have up to **25 replicas (100 connections)**. DNS records keep pointing at `<UUID>.cfargotunnel.com`. The tunnel ID and its routes do not change when a replica joins or leaves. How Cloudflare picks a replica explains most surprises. It sends traffic to an available connector, **generally the geographically closest one**, and the docs give **no guarantee** which. In practice, users near host B are served by host B over and over. A fault on one replica therefore shows up as a **regional** problem rather than a random one in a few percent of requests. ## The scenario The admin dashboard tunnel ran on host A. A second replica was started on host B, in another site, for availability. Since then, users served from B's region see two errors: - **`502` on `admin.example.com`.** The rule says `service: http://localhost:8080`. `localhost` means **the host `cloudflared` runs on**. The dashboard runs only on host A, so on host B the connection is refused and `cloudflared` returns a `502`. - **`404` on a hostname added last week.** Each replica of a locally-managed tunnel reads **its own `config.yml`**. The new rule was added on host A only, so on host B the request falls through to the catch-all, `http_status:404`. ## Diagnosing it 1. **`cloudflared tunnel info <NAME>`** lists each connector with its `CONNECTOR ID`, `CREATED`, `ARCHITECTURE`, `VERSION`, `ORIGIN IP` and `EDGE` location. A connector you did not expect, or one whose edge location matches the complaining region, stands out. 2. On each host, **`cloudflared tunnel ingress rule https://<hostname>/`** shows which rule that host's file would apply. Two hosts giving two answers is configuration drift. 3. The `cloudflared` logs on host B show `connect: connection refused` for the `502` requests. ## Fixing it - **Point services at addresses every replica can reach.** Use the service's private address or internal DNS name rather than `localhost`, or run the service on every host. - **Make the configuration identical everywhere.** Distribute one `config.yml` to every host, or move the tunnel to **remote management**, where Cloudflare pushes one configuration to every connector and all replicas serve the same routes. - **Roll configuration changes through replicas.** For a locally-managed tunnel, Cloudflare documents this sequence: 1. Start a replica with the new file. 2. Wait until it is running and serving. 3. Stop the old instance. Stopping it drops long-lived connections such as WebSockets and SSH, and UDP flows. - **Remove a stray connector** with `cloudflared tunnel cleanup --connector-id <ID> <NAME>`. ## Preventing it next time - **Treat replicas as identical units.** Build each from the same image or configuration source, and check them with `cloudflared tunnel ingress rule` for every published hostname after a change. - **Never use `localhost` for a service that runs on only one host.** Point rules at the address or internal name the service is reachable on from every replica. - **Watch connectors, not just the tunnel.** A tunnel stays Healthy while one replica returns errors, so alert on 5xx rates per hostname and review `cloudflared tunnel info` after deployments. - **Retire replicas deliberately.** Stop the process and, if its record lingers after an unclean shutdown, clean it up by connector ID so it no longer receives traffic. ## What replicas are not | Need | Replicas of one tunnel | Separate tunnels behind a load balancer | |---|---|---| | Survive a host failure | Yes | Yes | | Choose which site serves whom | No, the edge picks the closest | Yes, through steering | | Session affinity, health-based failover | No | Yes | | Appear to a load balancer as | One endpoint | One endpoint per tunnel | A load balancer treats all replicas of one tunnel UUID as a **single endpoint**. When the team needs steering, failover rules or affinity, it connects each site with its **own tunnel** and puts those tunnels in a pool; the load-balancing lesson covers pools and steering.
- Why does the failure look regional rather than random?Cloudflare does not spread a tunnel's requests evenly across replicas. It sends them to an available connector, generally the closest one, so the same users keep landing on the same replica. A broken replica fails consistently for the users nearest to it.
- How would you apply a config.yml change to a locally-managed tunnel without dropping traffic?Start a new replica with the updated file, wait until it is connected and serving, then stop the old instance. New requests move to the new replica. Long-lived WebSocket and SSH connections and UDP flows on the old one are dropped, so schedule the change for a quiet period.
- Cloudflare round-robins requests evenly across a tunnel's replicas
- localhost in a service URL means the host where the dashboard runs
- Every replica of a locally-managed tunnel reads one shared config from Cloudflare
- A load balancer can steer traffic between replicas of one tunnel
- Cloudflare detects a replica returning 502s and stops sending it traffic