# Behind a reverse proxy

A reverse proxy in front of qKVM can terminate HTTPS or sign people in. This page is for whoever sets one up, and says what qKVM needs from it.

## Set it up

1. Publish qKVM's port only on loopback (`-p 127.0.0.1:8787:8787`) or on a private container network. Only then leave out `--https`, and keep `--setup-code`.
2. Start qKVM with `--trusted-proxy` and the proxy's address or network: `--trusted-proxy 172.18.0.0/16`.
3. Have the proxy send `X-Forwarded-For` (the visitor's address) and `X-Forwarded-Proto` (whether the visit came over HTTPS).
4. Pass WebSocket connections on: the live view uses one.
5. Let the proxy close idle connections to qKVM sooner than 130 seconds.

For nginx, the parts that matter:

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

server {
    listen 443 ssl;
    location / {
        proxy_pass http://qkvm:8787;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

On a port other than 443, use `$http_host`, which keeps the port.

## Why it matters

- **Without `--trusted-proxy`, every visitor looks like the proxy.** Five wrong passwords by one person lock everybody out, and the activity log names the proxy.
- **Once a proxy is named, only that one is believed.** Name `127.0.0.1` too if one of your proxies runs on the same computer.
- **A proxy on the same computer is believed without being named** (`tailscale serve`, say), and must send `X-Forwarded-For`. One that sends nothing makes every visitor look local, and lets them through the first-run setup without the code.

## Idle connections

A proxy that keeps a connection to qKVM open longer than 130 seconds can lose a request, a press of a power button among them. These defaults are all fine:

| Proxy | Setting | Default |
|---|---|---|
| nginx | `keepalive_timeout` in an `upstream` block | 60 seconds |
| Traefik | `idleConnTimeout` | 90 seconds |
| cloudflared | `keepAliveTimeout` | 90 seconds |
| Caddy | `keepalive` | 2 minutes |

## Signing in at the proxy

To have the proxy sign people in (Authelia, authentik, oauth2-proxy, a company gateway), see [Single sign-on](https://qkvm.com/docs/web/single-sign-on).

## Related

- [How it is protected](https://qkvm.com/docs/web/how-it-is-protected)
- [Single sign-on](https://qkvm.com/docs/web/single-sign-on)
- [Install and run](https://qkvm.com/docs/web/run-it)

[Full reference](https://github.com/qkvm/qkvm/blob/main/GUIDE.md#how-it-is-protected)
