Why Keycloak usually sits behind a proxy
In production, Keycloak rarely faces the internet on its own. A reverse proxy or load balancer sits in front. It holds the TLS certificate, spreads traffic over several Keycloak nodes, and decides which paths the outside world may reach.
That middle layer changes what Keycloak sees. Requests arrive over plain HTTP from the proxy’s address, not over HTTPS from the visitor. Unless Keycloak is told how to read the proxy’s forwarded headers, it builds the wrong URLs, records the wrong client addresses and may refuse requests with "HTTPS required".
A good proxy config fixes three things at once: it sends the right headers, it exposes only the paths people need, and it handles the large headers that sign-in pages produce.
How Keycloak 26 reads a proxy
Keycloak trusts forwarded headers only when you ask it to. The option proxy-headers takes one of two values.
| Value | Headers Keycloak reads | Typical source |
|---|---|---|
xforwarded | X-Forwarded-For, -Proto, -Host, -Port | nginx, Caddy, Traefik, most load balancers |
forwarded | Forwarded (RFC 7239) | proxies configured for the standard header |
When TLS ends at the proxy, Keycloak also needs http-enabled=true, because production mode otherwise serves only HTTPS. With hostname set as a full URL, the public scheme, host and port come from that URL; the forwarded headers then mainly carry the visitor’s address and confirm the original scheme.
KC_HOSTNAME=https://auth.example.com
KC_HTTP_ENABLED=true
KC_PROXY_HEADERS=xforwardedKeycloak 26 also has proxy-trusted-addresses, which limits header trust to requests coming from listed addresses or ranges. Use it when the proxy has a stable address.
Which paths to expose
Keycloak serves very different things under one host: sign-in pages, token endpoints, the account console, the admin console and its API. Not all of them belong on the public internet.
| Path | What it serves | Expose publicly? |
|---|---|---|
/realms/ | Sign-in, tokens, discovery, account console | Yes |
/resources/ | Scripts, styles and images for those pages | Yes |
/admin/ | Admin console and Admin REST API | Only if you must, better on a separate host |
/realms/master/ | The master realm, used to manage the server | No, unless admins sign in from outside |
| Port 9000 | Health and metrics (management interface) | No |
Health checks and metrics run on the management port, 9000 by default, separate from the sign-in pages on 8080. The proxy should never forward to that port.
What this tool does
The tool writes a ready-to-edit config for nginx, Caddy or Traefik from three inputs: the public host name, the address where Keycloak listens, and whether the admin console should be reachable on that host.
- For nginx it writes a proxy snippet that overwrites the
X-Forwarded-*headers, a port 80 redirect to HTTPS, a 443 server block with certificate paths and larger proxy buffers, and locations for/realms/and/resources/only. - For Caddy it writes a site block that proxies the same paths and answers 404 for the rest. Caddy obtains the certificate and sets the forwarded headers itself.
- For Traefik it writes a dynamic configuration file with a router rule on the host and paths, a TLS certificate resolver and a service pointing at Keycloak.
- By default it blocks
/realms/master/. If you expose the admin console, it lets the master realm through, because the console signs in there.
Under the config it shows the matching Keycloak options, so both sides agree.
What it does not do
It does not get certificates, test the config or know your proxy’s version. Certificate paths in the nginx output follow the usual certbot layout and are placeholders. The Traefik output expects an entry point named websecure and a resolver named letsencrypt in your static configuration; rename them to match yours.
Choosing between nginx, Caddy and Traefik
All three work well in front of Keycloak. The choice usually follows what you already run.
nginx
The most common choice on a plain server. You manage certificates yourself, often with certbot, and you set every header explicitly. That makes the config longer but leaves nothing implicit.
Caddy
The shortest config. Caddy gets and renews certificates on its own and sets the forwarded headers by default. It suits a single server where you want few moving parts.
Traefik
A good fit when services already run in Docker or Kubernetes and Traefik discovers them. Routing lives in labels or dynamic files, and certificates come from a resolver you define once.
How to use it
- Pick nginx, Caddy or Traefik.
- Enter the public host name without
https://, for exampleauth.example.com. - Enter where Keycloak listens on HTTP, such as
127.0.0.1:8080or a container name and port. - Leave the admin console off unless people must manage realms through this host.
- Copy the config, adjust certificate paths or resolver names, and test it with the proxy’s own check before reloading.
- Set the Keycloak options shown under the config and restart Keycloak.
Where it helps
First production deploy on one server
A single VM with Keycloak in a container and nginx or Caddy on the host is a common first setup. Bind Keycloak to 127.0.0.1:8080 so only the proxy reaches it, then let the proxy face the internet.
Several Keycloak nodes behind a load balancer
With more than one node, the proxy spreads requests across them. Keycloak 26 shares sessions between nodes through its distributed caches, so sticky sessions are not required for correctness. Health checks for the pool should hit /health/ready on the management port from inside the network, not through the public host.
Moving to Kubernetes
In a cluster, an ingress controller or a Gateway API implementation plays the proxy role. The same rules apply: route /realms/ and /resources/, overwrite forwarded headers, and keep port 9000 internal. The generated nginx locations translate directly into path rules.
Upgrading from an older version
Older setups often used proxy=edge and served Keycloak under /auth. In Keycloak 26, proxy=edge is gone; use http-enabled=true and proxy-headers. If clients still call /auth/realms/..., either keep http-relative-path=/auth in Keycloak or rewrite the path in the proxy until every client is updated.
Common errors and fixes
upstream sent too big header while reading response header
Keycloak’s sign-in responses carry large cookies. nginx’s default buffers are too small for them. Raise proxy_buffer_size and proxy_buffers, as the generated config does.
Redirect loops or "HTTPS required"
Keycloak believes the visitor came over plain HTTP. Check that the proxy sends X-Forwarded-Proto: https, that Keycloak has proxy-headers=xforwarded, and that hostname is a full https:// URL.
Every visitor shows the proxy’s IP address
Events and brute force detection then treat all users as one address. Make sure the proxy sets X-Forwarded-For and that proxy-headers is set. If you use proxy-trusted-addresses, include the proxy’s real address.
404 on the admin console or on static files
The proxy does not pass /admin/ or /resources/. Add the missing location, or open the console on its own admin host.
502 or 504 from the proxy
The proxy cannot reach Keycloak at the upstream address, or Keycloak is still starting. Check the address and port, and look at /health/ready on the management port from the proxy host.
Security notes
- Overwrite forwarded headers in the proxy rather than appending to them. If the proxy passes on what the client sent, anyone can fake their address or scheme.
- Never set
proxy-headerson a Keycloak that clients can reach directly. Bind it to localhost or a private network. - Keep the master realm and the admin console off the public host. If admins need the console, give it its own host and limit access by network.
- Do not forward the management port. Health and metrics output describe your server in detail.
Best practices
- Test the config with
nginx -t,caddy validateor Traefik’s logs before reloading. - Use HTTP/2 or later between browsers and the proxy; plain HTTP/1.1 between the proxy and Keycloak is fine on a private network.
- Answer 404 for every path you do not route, so new Keycloak paths are not exposed by accident after an upgrade.
- Keep one config per public host. Mixing Keycloak with other apps under one host makes cookie and path rules harder to reason about.
- After any change, open the discovery document from outside and check that every URL uses your public address.