# Behind your own reverse proxy

> When nginx, Traefik or another proxy already holds ports 80 and 443, Klang’s Caddy goes behind it. What changes in the kit, what your proxy must pass on, and how Klang still sees each visitor’s IP.

The kit expects its Caddy on ports 80 and 443. If another proxy already holds them (nginx,
Traefik, Nginx Proxy Manager, another Caddy), Klang’s Caddy goes behind it. Your proxy handles
HTTPS for the four hostnames with its own certificates and passes every request on to Klang’s
Caddy, which keeps doing the routing.

## What changes in the kit

- **Caddy listens only on the machine itself**, on `127.0.0.1:8443`, instead of publishing 80 and 443.
- **Caddy signs its own certificates** (`compose.local.yml`), because only your proxy and Klang’s
  own services talk to it. The API reaches the files and voice through Caddy by their public
  hostnames, so Caddy keeps HTTPS inside. No browser sees those certificates.
- **Klang counts one more proxy** to find each visitor’s IP.

In `selfhost/.env`, leave `HTTP_PORT` and `HTTPS_PORT` unset and add:

```bash
CADDY_TRUSTED_PROXIES=private_ranges
TRUST_PROXY_HOPS=2
COMPOSE_FILE=docker-compose.yml:compose.local.yml:compose.override.yml
```

And create `selfhost/compose.override.yml` (Compose reads it, git ignores it):

```yaml
services:
  caddy:
    ports: !override
      - '127.0.0.1:8443:443'
```

`!override` replaces Caddy’s list of ports instead of adding to it (Compose 2.24 or later). With
`COMPOSE_FILE` in `.env`, the plain `docker compose` commands of the rest of the guide keep working.
Apply it with `docker compose up -d`.

- `CADDY_TRUSTED_PROXIES` is where your proxy connects from, as Caddy sees it. A proxy on the host
  reaches Caddy through Docker’s bridge, from a private address. `private_ranges` covers it, and
  it’s safe while Caddy is only published on `127.0.0.1`. If you publish Caddy anywhere else,
  list your proxy’s address instead.
- `TRUST_PROXY_HOPS=2` tells the API and the gateways that there are two proxies in front of
  them: yours and Caddy. They take the address your proxy saw. Without it, every visitor looks
  like your proxy, and they all share the rate limits.

If your proxy runs in Docker, attach it to the kit’s network (`klang-server_default`), point it at
`https://caddy:443`, and drop Caddy’s ports entirely (`ports: !reset []`).

## What your proxy must do

- **Send the four hostnames to `https://127.0.0.1:8443`**, with the hostname as SNI and the
  `Host` header as the browser sent it. Caddy’s certificate comes from its own authority: don’t
  verify it, or trust Caddy’s root certificate.
- **Pass WebSockets** on `/gateway` and `/bot-gateway` of the app’s hostname, and on the whole
  voice hostname (LiveKit’s signalling). Give them long timeouts: an hour is fine.
- **Accept large uploads on the files hostname.** Browsers upload files straight to it with a
  signed `PUT`, up to `LIMIT_UPLOAD_MB` (500 MB by default). The proxy must accept that body size,
  better without buffering it to disk first, and must not change the request: the signature
  covers the hostname, so a rewritten `Host` makes every upload fail with `403`.
- **Add the visitor’s address to `X-Forwarded-For`** (nginx: `$proxy_add_x_forwarded_for`;
  Traefik and Caddy do it by themselves). Klang takes the address your proxy added and ignores
  anything a visitor wrote in the header before.
- **Get the certificates and redirect port 80 to HTTPS.** Both are your proxy’s job now.

Calls don’t go through any proxy. LiveKit’s media ports (7881/TCP, 7882/UDP, 3478/UDP) stay
published by the kit and go straight to the machine
([Requirements](https://docs.klang.gg/self-hosting/requirements/#ports)).

## An example with nginx

On the host, with certificates for the four hostnames from your own ACME client:

```nginx
map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

server {
  listen 80;
  listen [::]:80;
  server_name chat.example.org admin.chat.example.org files.chat.example.org voice.chat.example.org;
  return 301 https://$host$request_uri;
}

server {
  listen 443 ssl;
  listen [::]:443 ssl;
  http2 on;
  server_name chat.example.org admin.chat.example.org files.chat.example.org voice.chat.example.org;

  ssl_certificate     /etc/letsencrypt/live/chat.example.org/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/chat.example.org/privkey.pem;

  # Klang applies its own upload limit. Don't buffer uploads or WebSockets.
  client_max_body_size 0;
  proxy_request_buffering off;
  proxy_buffering off;

  location / {
    proxy_pass https://127.0.0.1:8443;
    proxy_ssl_server_name on;
    proxy_ssl_name $host;
    proxy_http_version 1.1;
    proxy_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_read_timeout 1h;
    proxy_send_timeout 1h;
  }
}
```

An nginx on the host also answers over IPv6 and passes on each visitor’s real address, so the
kit’s network doesn’t need IPv6 in this setup ([IPv6](https://docs.klang.gg/self-hosting/requirements/#ipv6)).

## Cloudflare

- **The voice hostname must be DNS only** (the grey cloud): never proxied, never through a
  Cloudflare Tunnel. Both only carry HTTP, and the TURN relay on that hostname uses UDP.
- **The files hostname behind Cloudflare’s proxy** caps uploads at Cloudflare’s body limit (100 MB
  on the free plan). Leave it DNS only, or lower `LIMIT_UPLOAD_MB` to fit.
- We haven’t tested the kit with the app’s hostname behind Cloudflare’s proxy. There, Klang would
  see Cloudflare’s addresses instead of your visitors’. Keep the four hostnames DNS only.

## What we tested

nginx 1.28 in a container in front of the kit, on one computer: HTTPS at nginx with its own
certificate, the kit’s Caddy with its own authority behind it, `TRUST_PROXY_HOPS=2` and
`CADDY_TRUSTED_PROXIES=private_ranges`. Sign-up, a message over the gateway and an upload to the
files hostname worked. The API and the gateway saw the visitor’s address, not nginx’s (with
`TRUST_PROXY_HOPS=1`, they saw nginx’s), and an `X-Forwarded-For` written by the visitor was
ignored. We haven’t tested nginx on the host with a real domain, Traefik, Nginx Proxy Manager or
Cloudflare.
