# Reach your Hub with Cloudflare Tunnel

> Publish your Hub at your own domain through Cloudflare Tunnel without opening ports, and lock it with Cloudflare Access and a service token for the apps.

Source: https://docs.posternmail.app/reach/cloudflare/

This page publishes your Hub at a domain you manage on Cloudflare, for example
`https://mail.example.com`, without opening any port on your router. A small program,
`cloudflared`, runs next to the Hub and keeps an outbound connection to Cloudflare; Cloudflare
sends visitors' requests down that connection. The second half of the page adds Cloudflare
Access, so only you and your apps get past Cloudflare at all. For other options see
[Reaching your Hub](https://docs.posternmail.app/reach/).

> [!TIP]
> **Used the [installer](https://docs.posternmail.app/setup/)?** It does this for you when you choose **Cloudflare
> Tunnel**. It runs `cloudflared` with your tunnel token and sets the Hub's address and client IP
> header. Cloudflare Access (below) stays a manual step. To switch later, run the installer again
> and choose **Change how it is set up**.
> This page is for doing it by hand, or for understanding what was set up. With the installer, the
> Docker files are in `/opt/postern` instead of `~/postern`, and the program is
> `/usr/local/bin/postern-hub`.

You need:

- a Cloudflare account (the free plan is enough),
- a domain whose DNS is managed by Cloudflare (its name servers point to Cloudflare).

**Docker Compose:**
The simple path is `cloudflared` installed on the **host**, pointing at `127.0.0.1:8080`, which
`compose.yaml` publishes by default. That is what this page describes.
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
`cloudflared` runs on the same machine as the Hub and forwards to `127.0.0.1:8080`, where the
Hub listens by default.
**Desktop app (built-in Hub):**
This does not work with the desktop app's built-in Hub: it only accepts connections from your
own computer, and its address is fixed to `http://localhost:<port>`. To reach a Hub from other
devices, run it on a server as described in [Set up Postern](https://docs.posternmail.app/setup/), then pick that
method above.

## The trade-off: Cloudflare sees your traffic

With a tunnel, the HTTPS connection from your browser or phone ends at Cloudflare. Cloudflare
decrypts it, then sends it through the tunnel to your Hub. So everything the apps send and
receive passes through Cloudflare readable: your Hub password when you sign in, session tokens,
and the mail you read in the web app. The desktop and mobile apps fetch mail directly from your
providers, so their mail does not pass through the tunnel, but their sign-in and the mailbox
logins they get from the Hub do.

If that is not acceptable to you, use [Tailscale](https://docs.posternmail.app/reach/tailscale/) (end to end encrypted) or your
own [reverse proxy](https://docs.posternmail.app/reach/reverse-proxy/).

## 1. Install cloudflared

Install `cloudflared` from Cloudflare's own package repository. The commands for each
distribution, including the signing key, are on <https://pkg.cloudflare.com/index.html>.

**Docker Compose, Debian and Ubuntu and Raspberry Pi OS:**
On Debian, Ubuntu and Raspberry Pi OS, follow the section **Any Debian Based Distribution
(Recommended)**: it adds Cloudflare's apt repository and installs the `cloudflared` package.
On a Docker host running Fedora, follow the Fedora instructions below instead.
**Fedora and RHEL:**
Fedora has no section of its own on that page. Use **RHEL Generic**: it adds Cloudflare's
repository for dnf and installs the `cloudflared` package.
**Desktop app (built-in Hub):**
Not applicable to the built-in Hub.

> **Check:** `cloudflared --version` prints a version.

> **If it did not work:** Cloudflare changed its signing key in October 2025. If `apt update` or
> `dnf` complains about a signature, repeat the key step from the current instructions on
> <https://pkg.cloudflare.com/index.html>.

## 2. Create the tunnel

Sign in to Cloudflare. The command prints a link; open it, sign in and choose your domain:

```sh
cloudflared tunnel login
```

This saves a certificate to `~/.cloudflared/cert.pem`. Create a tunnel named `postern` and a
DNS record that points your hostname at it:

```sh
cloudflared tunnel create postern
cloudflared tunnel route dns postern mail.example.com
```

`tunnel create` prints the tunnel's ID and writes its credentials to
`~/.cloudflared/<tunnel-id>.json`. `route dns` creates a proxied `CNAME` record for
`mail.example.com` in your Cloudflare DNS. You do not need an `A` record.

> **Check:** `cloudflared tunnel list` shows `postern`, and the DNS page of your domain in the
> Cloudflare dashboard has a `CNAME` for `mail` pointing to `<tunnel-id>.cfargotunnel.com`.

> **If it did not work:** `route dns` fails if a record for that name already exists. Delete
> the old `A`, `AAAA` or `CNAME` record for `mail` in the dashboard and run it again.

## 3. Configure and start the tunnel as a service

The service reads its configuration from `/etc/cloudflared/`. Copy the tunnel's credentials
there:

```sh
sudo mkdir -p /etc/cloudflared
sudo cp ~/.cloudflared/<tunnel-id>.json /etc/cloudflared/
```

Create `/etc/cloudflared/config.yml` (`sudoedit /etc/cloudflared/config.yml`):

```yaml
tunnel: postern
credentials-file: /etc/cloudflared/<tunnel-id>.json
ingress:
  - hostname: mail.example.com
    service: http://127.0.0.1:8080
  - service: http_status:404
```

Replace `<tunnel-id>` with the ID from step 2. The last line answers every other hostname with
404. Install and start the service:

**Docker Compose, Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
```sh
sudo cloudflared service install
sudo systemctl enable --now cloudflared
```
**Desktop app (built-in Hub):**
Not applicable to the built-in Hub.

WebSockets (the Hub's live updates on `/api/v1/ws`) pass through the tunnel without extra
settings. Cloudflare's free plan accepts request bodies up to 100 MB, well above the Hub's
25 MB attachment limit.

> [!TIP]
> If you prefer the dashboard, you can instead create the tunnel under **Zero Trust → Networks →
> Tunnels**, add a public hostname `mail.example.com` with the service `http://localhost:8080`,
> and install it with the `sudo cloudflared service install <token>` command the dashboard
> shows. The Hub settings below are the same.

> **Check:** `sudo systemctl status cloudflared` shows `active (running)`, and from any machine
> `curl -s https://mail.example.com/api/v1/info` prints JSON with `"apiVersion":1`.

> **If it did not work:**
> - `502 Bad Gateway` or a Cloudflare error page "Bad gateway": the tunnel runs but the Hub does
>   not answer on `127.0.0.1:8080`. Check the Hub's log.
> - Error 1033: the tunnel is not connected. `sudo journalctl -u cloudflared -f` shows why;
>   usually a wrong `credentials-file` path.

## 4. Hub settings

The Hub must know its address, trust `cloudflared` as a proxy, and take the visitor's IP from
Cloudflare's `CF-Connecting-IP` header:

**Docker Compose:**
Add to `~/postern/.env`:

```sh
POSTERN_PUBLIC_URL=https://mail.example.com
POSTERN_TRUST_PROXY=true
```

`POSTERN_CLIENT_IP_HEADER` is not passed through by `compose.yaml`. Create
`~/postern/compose.override.yaml` (or add to it):

```yaml
services:
  hub:
    environment:
      POSTERN_CLIENT_IP_HEADER: CF-Connecting-IP
```

Leave `POSTERN_TRUSTED_PROXIES` unset. `cloudflared` connects to the published port, so the Hub
sees it at Docker's gateway address (a private address the default trusts), not at
`127.0.0.1`. Apply:

```sh
cd ~/postern && docker compose up -d
```

If you run `cloudflared` as a container in the same Compose project instead, use
`service: http://hub:8080` in its configuration; the settings above stay the same.
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
Add to `/etc/postern/hub.env` (`sudoedit /etc/postern/hub.env`):

```ini
POSTERN_LISTEN=127.0.0.1:8080
POSTERN_PUBLIC_URL=https://mail.example.com
POSTERN_TRUST_PROXY=true
POSTERN_TRUSTED_PROXIES=127.0.0.1,::1
POSTERN_CLIENT_IP_HEADER=CF-Connecting-IP
```

Apply:

```sh
sudo systemctl restart postern-hub
```
**Desktop app (built-in Hub):**
Not applicable to the built-in Hub.

`CF-Connecting-IP` carries the visitor's real address. The Hub only reads it from a trusted
proxy and uses it for rate limits, the security log and the device list.

> **Check:** open `https://mail.example.com`, sign in and look at **Settings → Security**: the
> newest sign-in shows your own public IP address, not `127.0.0.1` or a `172.x` address.
> `postern-hub doctor` shows `✓ https://mail.example.com is reachable`.

> **If it did not work:** "Cross-origin request rejected" when you change something means
> `POSTERN_PUBLIC_URL` does not match the address in the browser. A wrong IP in the security log
> means `POSTERN_CLIENT_IP_HEADER` or `POSTERN_TRUST_PROXY` did not reach the Hub (Docker: check
> `compose.override.yaml` is next to `compose.yaml`).

## Lock it with Cloudflare Access

So far anyone who knows the address reaches your Hub's sign-in page. Cloudflare Access puts a
second lock in front of it: browsers must pass Cloudflare's own login (for example a one-time
code sent to your email), and the apps get in with a **service token** that the Hub hands to
them after they sign in. It never replaces the Hub's own sign-in.

The menu names below are those of the Cloudflare Zero Trust dashboard at the time of writing;
Cloudflare renames them from time to time.

### 1. Create a service token

In **Zero Trust → Access → Service credentials → Service tokens**, create a token, for example
"Postern apps", with the duration **Non-expiring** (or an expiry you are willing to rotate).
Copy the **Client ID** and the **Client Secret**. The secret is shown only once.

### 2. Create the Access application

In **Access → Applications → Add an application → Self-hosted**:

- Domain: `mail.example.com`, the whole host, with no path. A path-only application would leave
  the rest of the Hub open.
- Identity provider: **One-time PIN**, or your own.
- Session duration: as you like.

Add two policies, **in this order**:

| Order | Action | Include | Who it is for |
|---|---|---|---|
| 1 | **Service Auth** | **Service Token** = the token from step 1 | The apps |
| 2 | **Allow** | **Emails** = your address(es), for example `you@example.com` | Browsers |

Leave **Return 401 response for Service Auth policies** off. The apps recognize Access by its
redirect to the Cloudflare sign-in page; a `401` would look to them like a revoked session.

### 3. Give the token to the Hub

**Docker Compose:**
Add to `~/postern/compose.override.yaml`, under `services: hub: environment:`:

```yaml
services:
  hub:
    environment:
      POSTERN_CLIENT_IP_HEADER: CF-Connecting-IP
      POSTERN_ACCESS_CLIENT_ID: "<client id>.access"
      POSTERN_ACCESS_CLIENT_SECRET: "<client secret>"
```

The file now holds a secret: `chmod 600 ~/postern/compose.override.yaml`. Apply:

```sh
cd ~/postern && docker compose up -d
```
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
Add to `/etc/postern/hub.env` (it is `root:postern`, mode `0640`, so other users cannot read
the secret):

```ini
POSTERN_ACCESS_CLIENT_ID=<client id>.access
POSTERN_ACCESS_CLIENT_SECRET=<client secret>
```

Apply:

```sh
sudo systemctl restart postern-hub
```
**Desktop app (built-in Hub):**
Not applicable to the built-in Hub.

Both must be set together. With only one, the Hub refuses to start with
`POSTERN_ACCESS_CLIENT_ID and POSTERN_ACCESS_CLIENT_SECRET must be set together`.

The Hub hands the token only to apps that are already signed in: in the pairing QR code (shown
in the signed-in web app), in the app sign-in redirect and in every sign-in result. The desktop
and mobile apps then send `CF-Access-Client-Id` and `CF-Access-Client-Secret` on every request
to the Hub, WebSockets included.

### 4. Connect your devices behind Access

- **Browsers:** open `https://mail.example.com`, pass Cloudflare's login (email code), then sign
  in to the Hub as usual.
- **Phones:** pair them by **scanning the QR code** in the web app (**Settings → Devices →
  Connect a device**). The QR code carries the token. Typing a pairing code cannot work behind
  Access, because the phone has no token yet and Cloudflare turns it away. See
  [Pairing](https://docs.posternmail.app/apps/pairing/).
- **Desktop app and "sign in with the browser":** enter the address as usual. The sign-in window
  shows Cloudflare's email step once, then the app receives the token from the Hub.

### 5. Check it

From outside your network, without the token every request is redirected to Cloudflare:

```sh
curl -sI https://mail.example.com/api/v1/info | head -1
```

```text
HTTP/2 302
```

With the token you reach the Hub:

```sh
curl -s -H "CF-Access-Client-Id: <client id>.access" -H "CF-Access-Client-Secret: <client secret>" https://mail.example.com/api/v1/info
```

The answer is JSON with `"apiVersion":1`. `postern-hub doctor` does the same check with the
configured token and reports `✓ https://mail.example.com is reachable through Cloudflare Access
with the apps' service token`.

> **If it did not work:**
> - doctor says "Cloudflare Access in front of https://mail.example.com rejected the configured
>   service token (POSTERN_ACCESS_CLIENT_ID)": the token is not in the **Service Auth** policy,
>   is expired or revoked, or the ID or secret has a typo.
> - doctor says "is behind an access gateway (Cloudflare Access) that turned this check away":
>   the two `POSTERN_ACCESS_*` variables did not reach the Hub.
> - An app says "the access gateway in front of your Hub (Cloudflare Access) turned this device
>   away; pair it again to get a fresh access token": the device has an old token. Pair it again
>   with the QR code. The app keeps working with its local mail meanwhile and is not signed out.
> - Browsers get in but apps do not: the **Allow** policy is above the **Service Auth** policy,
>   or **Return 401 response for Service Auth policies** is on.

### Rotating the token

1. Create a new service token.
2. Add it to the **Service Auth** policy next to the old one.
3. Put the new ID and secret into the Hub's settings and restart the Hub.
4. Pair every phone again by scanning the QR code, and sign in again on desktop apps.
5. Delete the old token in Cloudflare.

Devices that still carry the old token after step 5 are turned away with the message above
until you pair them again.
