# Proxy auth — Cloudflare Access + Tunnel

Source: https://docs.chmonitor.dev/guide/guides/proxy-auth-cloudflare-access

> Put chmonitor behind Cloudflare Access and a Cloudflare Tunnel for Zero Trust login, using the proxy auth provider with signed JWT verification.

Cloudflare Access sits in front of chmonitor and does the real login (SSO, email OTP, GitHub/Google, etc.). It signs every authenticated request with a `Cf-Access-Jwt-Assertion` JWT. chmonitor's `proxy` auth provider verifies that JWT cryptographically against your team's JWKS — there is no shared secret to manage, the signature itself is the proof.

**When to pick this path**

  Use this when you already run (or want) Cloudflare Zero Trust, don't want to run your own OIDC/ForwardAuth service, and are fine exposing chmonitor to the internet only through a Cloudflare Tunnel (no inbound ports opened). Compare this setup with the other two in [Proxy & SSO auth setup](/guide/guides/proxy-auth-setup).

|&#x22;1. Request&#x22;| CF[&#x22;Cloudflare Access&#x22;]
CF -->|&#x22;2. Login (SSO / OTP / IdP)&#x22;| B
B -->|&#x22;3. Cf-Access-Jwt-Assertion&#x22;| TUN[&#x22;cloudflared Tunnel&#x22;]
TUN -->|&#x22;4. Forward&#x22;| CHM[&#x22;chmonitor (proxy provider)&#x22;]
CHM -->|&#x22;5. Verify JWT via JWKS&#x22;| CF`"
/>

## Prerequisites [#prerequisites]

* A Cloudflare account with **Zero Trust** enabled (Access is available on the Free plan for small teams).
* A domain managed on Cloudflare (for the Tunnel's public hostname).
* `cloudflared` installed on the host/container that can reach chmonitor (`cloudflared tunnel` runs as a sidecar or on the same box).
* chmonitor running somewhere `cloudflared` can reach over plain HTTP (Docker, Kubernetes Service, or bare metal) — it does **not** need to be reachable from the public internet directly.

**Do not also expose chmonitor's port publicly**

  Access only protects requests that arrive through the Tunnel. If chmonitor's port is also reachable directly (a public LoadBalancer, a port-forwarded router, a Service of type NodePort), that path bypasses Access entirely. Keep chmonitor bound to localhost/an internal network and let the Tunnel be the only ingress.

## Setup [#setup]

  
    ### Create an Access application [#create-an-access-application]

    In the Cloudflare Zero Trust dashboard:

    1. **Access → Applications → Add an application → Self-hosted**.
    2. Set the **Application domain** to the hostname you'll route through the Tunnel, e.g. `chmonitor.example.com`.
    3. Under **Session duration**, pick a reasonable window (e.g. 24 hours) — this controls how often users have to re-authenticate.
    4. Add an **Access policy**: e.g. "Allow" → Include → Emails ending in `@yourcompany.com`, or a specific Google/GitHub/SAML group.
    5. Save, then open the application's overview page and copy the **Application Audience (AUD) tag** — a 64-character hex string. You'll need it for `CHM_CF_ACCESS_AUD`.
    6. Note your **team domain** too (Zero Trust → Settings → Custom Pages, or the URL pattern `https://<your-team>.cloudflareaccess.com`).
  

  
    ### Create a Cloudflare Tunnel [#create-a-cloudflare-tunnel]

    ```bash
    cloudflared tunnel login
    cloudflared tunnel create chmonitor
    cloudflared tunnel route dns chmonitor chmonitor.example.com
    ```

    This creates a tunnel named `chmonitor`, provisions a DNS record for `chmonitor.example.com` pointing at the tunnel, and writes credentials to `~/.cloudflared/<tunnel-id>.json`.
  

  
    ### Configure the tunnel ingress [#configure-the-tunnel-ingress]

    Create `~/.cloudflared/config.yml` (or mount it into the `cloudflared` container):

    ```yaml
    tunnel: chmonitor
    credentials-file: /etc/cloudflared/<tunnel-id>.json

    ingress:
      - hostname: chmonitor.example.com
        service: http://chmonitor:8080   # chmonitor's internal address/port
      - service: http_status:404          # catch-all, required as the last rule
    ```

    Running as a container alongside chmonitor (docker-compose extract):

    ```yaml
    services:
      chmonitor:
        image: ghcr.io/chmonitor/chmonitor:latest
        # do not publish chmonitor's port on the host — the tunnel is the only ingress
        environment:
          CHM_AUTH_PROVIDER: proxy
          CHM_CF_ACCESS_TEAM_DOMAIN: https://your-team.cloudflareaccess.com
          CHM_CF_ACCESS_AUD: <64-char-aud-tag>

      cloudflared:
        image: cloudflare/cloudflared:latest
        command: tunnel --config /etc/cloudflared/config.yml run
        volumes:
          - ./cloudflared:/etc/cloudflared
        depends_on:
          - chmonitor
    ```

    Run it directly instead of via compose:

    ```bash
    cloudflared tunnel --config ~/.cloudflared/config.yml run chmonitor
    ```
  

  
    ### Configure chmonitor [#configure-chmonitor]

    

    ```bash
    CHM_AUTH_PROVIDER=proxy
    CHM_CF_ACCESS_TEAM_DOMAIN=https://your-team.cloudflareaccess.com
    CHM_CF_ACCESS_AUD=<64-char-aud-tag>
    ```

    On every request chmonitor verifies the `Cf-Access-Jwt-Assertion` header: checks the `RS256` signature against `https://your-team.cloudflareaccess.com/cdn-cgi/access/certs` (the JWKS endpoint, cached \~1 hour), then checks `aud` matches `CHM_CF_ACCESS_AUD`, `iss` matches the team domain, and `exp` hasn't passed. The authenticated subject is the JWT's `email` claim (falls back to `sub`). No shared secret is involved — an unset or wrong `CHM_CF_ACCESS_AUD`/`CHM_CF_ACCESS_TEAM_DOMAIN` simply disables the mechanism (fails closed).

    Set these as Worker secrets in production rather than plaintext config:

    ```bash
    wrangler secret put CHM_CF_ACCESS_TEAM_DOMAIN
    wrangler secret put CHM_CF_ACCESS_AUD
    ```
  

  
    ### Redeploy and verify [#redeploy-and-verify]

    Open `https://chmonitor.example.com` in a browser — Cloudflare Access should show its login page before you ever reach chmonitor. After authenticating, you land on the dashboard with data loading normally.

    ```bash
    curl -i https://chmonitor.example.com/api/v1/auth/me
    # → 401

    # A request carrying a valid Access JWT (e.g. copied from browser dev tools,
    # or via `cloudflared access curl`) returns the authenticated identity
    cloudflared access curl https://chmonitor.example.com/api/v1/auth/me
    ```
  

## Troubleshooting [#troubleshooting]

  
#### Every request returns 401 even after logging in through Access

    Confirm `CHM_CF_ACCESS_TEAM_DOMAIN` has no trailing slash mismatch with the JWT's `iss` claim, and that `CHM_CF_ACCESS_AUD` exactly matches the AUD tag on the Access application (not the account ID or a different application's tag). A stale worker deploy that predates setting the secrets will also 401 — redeploy after `wrangler secret put`.
  

  
#### JWKS fetch failures / intermittent 401s right after key rotation

    Cloudflare rotates signing keys roughly every 6 weeks; previous keys stay valid for 7 days after rotation. chmonitor caches the whole JWKS document for about 1 hour, so a rotation is picked up within that window automatically — no redeploy needed. If a client presents a genuinely unknown `kid` (forged or very stale token), chmonitor does not force a fresh JWKS fetch on that specific miss (a deliberate DoS mitigation); it will pick up new keys on the next natural cache refresh.
  

  
#### Header spoofing risk if the Tunnel is bypassed

    The whole security model rests on `Cf-Access-Jwt-Assertion` only ever arriving from Cloudflare's edge. If chmonitor is *also* reachable directly (a public port, a misconfigured Kubernetes Service, a firewall rule opened for debugging), a client can send an arbitrary `Cf-Access-Jwt-Assertion` header — but verification will fail because they cannot produce a valid RS256 signature over their forged payload, so this specific mechanism doesn't have a forgery risk the way trusted-header does. The real risk is exposing an *unauthenticated* path to chmonitor in parallel (e.g. a raw port with no Access in front of it at all). Only the Tunnel should route to chmonitor.
  

  
#### Websocket / streaming passthrough for the AI agent chat

    The agent chat streams its response as a long-lived HTTP request (SSE-style, not a websocket upgrade). Cloudflare Tunnel and Access both pass through streaming responses transparently — Access checks the JWT once, at the start of the request, not per chunk. No special `ingress` config is required beyond the standard `service: http://...` entry above. If you see the chat stall or truncate, check chmonitor's own request timeout config rather than the tunnel.
  

  
#### No sign-in button appears in the chmonitor header

    That's expected. With `CHM_AUTH_PROVIDER=proxy` there is nothing else to set for the browser, and Access handles the login page entirely outside chmonitor's UI, so chmonitor never renders its own sign-in button for this path.
  

## Related [#related]

  - [Proxy auth overview](/guide/guides/proxy-auth-setup): Compare all three proxy patterns.

  - [nginx + oauth2-proxy](/guide/guides/proxy-auth-nginx-oauth2-proxy): Self-hosted OIDC login in front of nginx.

  - [Traefik ForwardAuth + OIDC](/guide/guides/proxy-auth-traefik-oidc): Traefik middleware forwarding an OIDC session.

  - [Cloudflare Access reference](/operate/authentication/cloudflare-access): Full env var reference for the proxy provider.

  - [Authentication overview](/operate/authentication): Compare all chmonitor auth providers.