Skip to content

Keycloak reverse proxy config generator

Generate an nginx, Caddy or Traefik configuration for Keycloak that passes only the paths people need and sets the forwarded headers Keycloak trusts.

Runs in your browser. Nothing you paste is sent to us.

Setup

A made-up host is filled in.

Where Keycloak listens on HTTP (port 8080 by default). Never the management port 9000.

Proxy config

nginx
# /etc/nginx/snippets/keycloak-proxy.conf
proxy_http_version 1.1;
proxy_set_header Host $host;
# Overwrite, never append: Keycloak trusts these headers.
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;

# /etc/nginx/sites-available/keycloak
upstream keycloak {
    server 127.0.0.1:8080;
}

server {
    listen 80;
    listen [::]:80;
    server_name auth.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name auth.example.com;
    ssl_certificate     /etc/letsencrypt/live/auth.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/auth.example.com/privkey.pem;

    # Keycloak's login pages send large cookies and headers.
    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;

    location ^~ /realms/master/ { return 404; }

    location /realms/ {
        proxy_pass http://keycloak;
        include snippets/keycloak-proxy.conf;
    }
    location /resources/ {
        proxy_pass http://keycloak;
        include snippets/keycloak-proxy.conf;
    }
    location / { return 404; }
}
Keycloak options to match
KC_HOSTNAME=https://auth.example.com
KC_HTTP_ENABLED=true
KC_PROXY_HEADERS=xforwarded
Only the paths people need
The config passes /realms/ (sign-in, tokens, account) and /resources/ (scripts, styles, images) and answers 404 for the rest. Health and metrics stay on port 9000, off the public address.
Get the certificate first (for example with certbot), then test with nginx -t before you reload.

How to use it

  1. Pick your proxy.
  2. Enter the public host name and where Keycloak listens.
  3. Decide whether the admin console is reachable on this host.
  4. Copy the proxy config and the matching Keycloak options.

What to know

Behind a proxy, Keycloak learns the visitor’s address and the original scheme and host from X-Forwarded headers. Set proxy-headers=xforwarded in Keycloak, and make the proxy overwrite these headers so a client cannot fake them.

Sign-in, tokens and the account console live under /realms/, and scripts and styles under /resources/. Everything else, including the admin console, can stay off the public address.

Keycloak’s login pages send large cookies and headers, so the nginx config raises the proxy buffer sizes. Health and metrics stay on the management port 9000 and are never proxied.

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.

ValueHeaders Keycloak readsTypical source
xforwardedX-Forwarded-For, -Proto, -Host, -Portnginx, Caddy, Traefik, most load balancers
forwardedForwarded (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.

Keycloak options to match the proxy
KC_HOSTNAME=https://auth.example.com
KC_HTTP_ENABLED=true
KC_PROXY_HEADERS=xforwarded

Keycloak 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.

PathWhat it servesExpose publicly?
/realms/Sign-in, tokens, discovery, account consoleYes
/resources/Scripts, styles and images for those pagesYes
/admin/Admin console and Admin REST APIOnly if you must, better on a separate host
/realms/master/The master realm, used to manage the serverNo, unless admins sign in from outside
Port 9000Health 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

  1. Pick nginx, Caddy or Traefik.
  2. Enter the public host name without https://, for example auth.example.com.
  3. Enter where Keycloak listens on HTTP, such as 127.0.0.1:8080 or a container name and port.
  4. Leave the admin console off unless people must manage realms through this host.
  5. Copy the config, adjust certificate paths or resolver names, and test it with the proxy’s own check before reloading.
  6. 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-headers on 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 validate or 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.
Questions

Frequently asked questions

Which paths should a proxy expose for Keycloak?

`/realms/` and `/resources/` are enough for sign-in and apps. Expose `/admin/` only if people manage realms from outside, and preferably on a separate admin host name.

Why do I get "upstream sent too big header" from nginx?

Keycloak’s responses carry large cookies. Raise `proxy_buffer_size` and `proxy_buffers`, as the generated config does, then reload nginx.

What replaced proxy=edge in Keycloak 26?

Set `http-enabled=true` so Keycloak accepts plain HTTP from the proxy, and `proxy-headers=xforwarded` or `forwarded` so it reads the proxy’s headers. Set `hostname` to the public URL as well.

Does Keycloak need sticky sessions behind a load balancer?

Keycloak 26 shares session data between nodes through its distributed caches, so requests can land on any node. Some teams still enable stickiness to reduce cross-node traffic, but it is not needed for sign-in to work.

Do I need X-Forwarded-Port?

Keycloak reads it with `proxy-headers=xforwarded`. When `hostname` is a full URL, the URL decides the public port, so the header matters less. Sending it does no harm when the proxy overwrites it.

Should the health check go through the proxy?

No. Health and metrics are on the management port, 9000 by default. Check them from inside the network and keep that port off the public address.

Can I serve Keycloak under a path like /auth?

Yes. Either set `http-relative-path=/auth` so Keycloak serves under that path, or rewrite the path in the proxy. Include the path in `hostname` so tokens and links carry it.

Does Caddy need extra header settings for Keycloak?

Usually not. Caddy’s `reverse_proxy` sets `X-Forwarded-For`, `X-Forwarded-Proto` and `X-Forwarded-Host` by default and ignores those headers from clients unless you configure trusted proxies.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.