
# HTTPS and reverse proxies

Serve the gateway and its dashboard at an `https://` address, with sign-in and streamed responses working through your proxy.

Store the certificate in a TLS Secret, add the public URL and the ingress to your values file, then apply it:

```bash
kubectl -n tokedex create secret tls gateway-tls \
  --cert=gateway.crt --key=gateway.key
```

```yaml
publicUrl: https://gateway.example.com
ingress:
  enabled: true
  className: <ingress-class>
  host: gateway.example.com
  tlsSecretName: gateway-tls
  annotations: {}
```

```bash
helm upgrade --install tokedex ./helm/tokedex \
  --namespace tokedex \
  -f release-values.yaml \
  --wait --timeout 10m
curl -sS https://gateway.example.com/readyz
```

```json
{"ok":true,"release":{"environment":"local"},"service":"tokedex"}
```

You need: the release and `release-values.yaml` from [Deploy to production](/gateway/deploy), an ingress controller in the cluster, and a certificate and key for the hostname. A certificate manager that writes the same Secret works too, and is the better choice because it renews the certificate before it expires.

## Public URL

The public URL is the origin browsers use, including the scheme, host and any port, such as `https://gateway.example.com`. Serve the dashboard and sign-in at the root of that hostname. The gateway accepts the configured `Host`. It also accepts `localhost`, `127.0.0.1` and `[::1]` on any port when you leave the public URL unset or when either the public URL or the gateway's listen address is loopback. Other hosts get `421`.

A public URL with a path stops the gateway at start-up and prints this error.

```text
TOKEDEX_PUBLIC_URL must be the http(s) origin browsers use to reach this gateway, such as https://gateway.example.com: it has a path, query or fragment
```

Invitation links, single sign-on redirect URIs and Slack's Redirect URL are built on it. Settle the final hostname before you set up single sign-on or Slack alerts, so neither needs redoing. If you change it, update the redirect URI at your identity provider and add the new Redirect URL that **Alerts → Delivery** shows to your [Slack app](/gateway/budget-alerts#post-alerts-to-slack).

## Proxy settings

Any reverse proxy in front of the gateway needs these settings. Set them in `ingress.annotations`, in your controller's own annotation names.

| Setting | Value |
| --- | --- |
| Request body size limit | At least 12,000,000 bytes, the gateway's own limit |
| Response buffering | Off, so streamed responses arrive as the model writes them |
| Read and send timeouts | 300 seconds or more, for requests that stay silent that long |
| `Host` header | Passed to the gateway unchanged, so the gateway can compare it with the public URL |
| `X-Forwarded-For` header | The client's address, written by the proxy. The first proxy replaces any value the client sent. |

The gateway reads the public host from `Host` only. `X-Forwarded-Host` and `X-Forwarded-Proto` have no effect.

In an NGINX that runs outside the cluster, a server block sets them like this. Point `proxy_pass` at the address where NGINX reaches the gateway's Service; the example uses `127.0.0.1:4000`.

```nginx
server {
    listen 443 ssl;
    server_name gateway.example.com;

    ssl_certificate /etc/nginx/tls/gateway.crt;
    ssl_certificate_key /etc/nginx/tls/gateway.key;

    client_max_body_size 12000000;

    location / {
        proxy_pass http://127.0.0.1:4000;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Connection "";

        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }
}
```

NGINX's default body limit of 1 MiB refuses a larger request with `413`.

If you list gateway replicas directly in an NGINX `upstream` block, set a short upstream connection timeout such as `proxy_connect_timeout 1s`. NGINX can then try a healthy replica sooner if it cannot connect to the first one. Keep the read and send timeouts for established connections as shown above.

Leave the `Origin`, `Cookie` and `Authorization` headers unchanged. If you raise the gateway's body limit with `TOKEDEX_MAX_REQUEST_BYTES`, raise the proxy's to match. Keep the query string of `/alerts/slack/` out of your access logs: Slack returns there with a single-use code.

Restrict who can reach the dashboard, `/control` and `/observability` with your network, a VPN or an authenticating proxy, since they administer the gateway. Clients need only `/v1`, Gemini GenerateContent included, and `/api/hello`, which Claude Code checks.

## Trusted proxies

The gateway limits sign-in attempts for each client address. It reads that address from `X-Forwarded-For` only on a connection from a proxy listed in `TOKEDEX_TRUSTED_PROXIES`, as IP addresses or CIDR ranges separated by commas or spaces.

List the addresses your ingress controller's Pods connect from, or the address an outside proxy's connections arrive from, as an entry of the `extraEnv` list in `release-values.yaml`, and apply the values again:

```yaml
extraEnv:
  - name: TOKEDEX_TRUSTED_PROXIES
    value: "<ingress-controller-pod-cidr>"
```

List only proxies you operate, since a listed proxy can report any address. Behind a chain of proxies, list each one and have each append to `X-Forwarded-For`; the gateway takes the right-most address that is not a trusted proxy. While the setting is unset, the gateway turns this limit off for connections from private and loopback addresses, and logs a warning at the first sign-in.

> **Important:** Set `TOKEDEX_TRUSTED_PROXIES` behind an ingress. Its Pods connect from private addresses, so while the setting is unset, nothing limits sign-in attempts from any client.

## Check sign-in and streaming

1. Open `https://gateway.example.com` and sign in.
2. Create an [API key](/gateway/api-keys). It saves without an error, so cookies and headers pass through.
3. Open **Account → Sessions**. **From** shows the client's network, not the proxy's.
4. Send a streamed request from **Playground** in the dashboard. The reply appears as the model writes it. The request calls your provider and can incur charges.

Sign-in with a password or single sign-on needs `https`. Over plain HTTP from another machine it reports **Passwords are only accepted over https**. `TOKEDEX_ALLOW_HTTP_SIGN_IN=1` allows it anyway and sends passwords unencrypted, so fix HTTPS instead of setting it on a gateway that other machines reach.

Next: [Single sign-on](/gateway/single-sign-on), [Deploy to production](/gateway/deploy) or [Troubleshooting](/gateway/troubleshooting).
