# Cloudflare Tunnel

URL: /docs/guides/cloudflare-tunnel

Source: https://github.com/easypanel-io/monorepo/blob/main/apps/website/content/docs/guides/cloudflare-tunnel/index.mdx

Expose App and Box services through a Cloudflare Tunnel without opening ports on the server.



A [Cloudflare Tunnel](https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel/)
sends traffic from Cloudflare to your server over an outgoing connection, so
your services can be reached on your domains without opening ports 80 and 443
to the internet or publishing the server's IP address in DNS.

Easypanel runs the `cloudflared` connector as a service and manages the
tunnel's routes and DNS records for you. Each route is a **tunnel rule** that
points a hostname on one of your Cloudflare domains to a port of an App or Box
service.

<Callout type="warn">
  Cloudflare Tunnel support is experimental. Test it on a non-critical service
  before moving production traffic to it.
</Callout>

## Requirements [#requirements]

* A Cloudflare account with at least one domain (zone) on Cloudflare DNS.
* An API token with these permissions:
  * **Account → Account Settings → Read**
  * **Account → Cloudflare Tunnel → Edit**
  * **Zone → Zone → Read**
  * **Zone → DNS → Edit**

To create the token, go to
[dash.cloudflare.com/profile/api-tokens](https://dash.cloudflare.com/profile/api-tokens),
select **Create Token**, then **Create Custom Token**, and add the permissions
above.

## Create a tunnel [#create-a-tunnel]

1. Open the [Cloudflare One dashboard](https://one.dash.cloudflare.com) and
   select your account.
2. Go to **Networks → Tunnels** and select **Create a tunnel**.
3. Select **Cloudflared** as the connector type.
4. Name the tunnel and select **Save tunnel**. You do not need to install the
   connector yourself; Easypanel runs it.

## Connect Easypanel [#connect-easypanel]

1. Open **Settings → Server → Cloudflare Tunnel**.
2. Paste the token into **API Token**.
3. Select your account in **Accounts**, or enter the **Account ID**.
4. Select the tunnel in **Tunnels**, or enter the **Tunnel ID**.
5. Optionally set the **Region**. See [Region](#region).
6. Select **Save**, then select **Start Tunnel**.

**Stop Tunnel** removes the connector service. The saved configuration and
tunnel rules are kept, so **Start Tunnel** brings them back.

### Region [#region]

By default, `cloudflared` connects to Cloudflare's global network. Set
**Region** to connect only to a specific region, for example `us` for data
centers in the United States. Leave it empty to use the global region. See the
[Cloudflare documentation](https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel/configure-tunnels/cloudflared-parameters/run-parameters/#region)
for supported values.

Changing the region of a running tunnel restarts the connector, which briefly
interrupts traffic through the tunnel.

## Add a tunnel rule [#add-a-tunnel-rule]

Tunnel rules are managed from the **Domains** tab of an App or Box service. The
**Cloudflare Tunnel Rules** panel appears there while the tunnel is running.

1. Open the service and select the **Domains** tab.
2. In **Cloudflare Tunnel Rules**, select **Add Tunnel Rule**.
3. Select the **Domain** and enter an optional **Subdomain**. Leave the
   subdomain empty to use the domain itself.
4. Optionally enter a **Path** to route only matching requests.
5. Select the **Internal Protocol** and enter the **Internal Port** the
   application listens on inside the container.
6. Select **Create**.

Easypanel creates a proxied `CNAME` record for the hostname that points to the
tunnel, and adds the route to the tunnel configuration. Editing a rule updates
both, and removing a rule deletes the DNS record and the route. Deleting a
service removes its tunnel rules.

Routes that you add to the tunnel outside Easypanel, for example in the
Cloudflare dashboard, are kept when Easypanel updates the tunnel configuration.

## Troubleshooting [#troubleshooting]

* **The Cloudflare Tunnel Rules panel is missing**: the tunnel is not running.
  Select **Start Tunnel** in **Settings → Server → Cloudflare Tunnel**.
* **No accounts, tunnels, or domains are listed**: check that the API token
  has all the permissions listed in [Requirements](#requirements).
* **Creating a rule fails**: check that no DNS record already exists for that
  hostname in Cloudflare. Remove it, or choose another subdomain.
* **The hostname returns a 404 or a 502 error**: check that the service is
  running and that the internal port and protocol match what the application
  listens on.
