Skip to main content

Configure Keycloak Behind a Reverse Proxy

Four settings decide whether Keycloak works behind a proxy. Set them on the server, not in the proxy:

conf/keycloak.conf
hostname=https://auth.example.com
proxy-headers=xforwarded
proxy-trusted-addresses=10.0.1.7
http-enabled=true

hostname fixes the URLs Keycloak publishes — the issuer, the authorization endpoint, every link in every email. proxy-headers makes Keycloak read X-Forwarded-* at all, which is what gets the real client IP into the event log and the brute-force detector. proxy-trusted-addresses says which hop is allowed to set those headers. And on the proxy side there is exactly one line that matters more than the rest: overwrite X-Forwarded-For, never append to it, or any client can tell Keycloak what IP address it is coming from.

Tested against

Keycloak 26.8.2 (quay.io/keycloak/keycloak:26.8.2), behind nginx 1.29-alpine, Caddy v2.11.9 and Traefik v3.8.0, on a 2-vCPU host. Every URL, status code, header value and log line below is copied from a run on 2026-11-30.

The reference page is Keycloak's Using a reverse proxy. It is accurate and it lists every option. What it does not give you is which combinations actually work, what each wrong one looks like from the outside, or a proxy config you can paste. That is what this page is.

What goes wrong, and why nothing tells you​

Start a proxy in front of Keycloak with the configuration most guides give you — TLS at the edge, proxy_pass to the container — and everything looks healthy. The login page renders, nginx logs 200s, Keycloak logs nothing unusual. Then ask Keycloak what it thinks its own address is:

curl -s https://auth.example.com/realms/app/.well-known/openid-configuration \
| jq -r .issuer
http://kc:8080/realms/app

That is the Docker container name, over plaintext, published to every OIDC client on your network as the address to send users to. Nothing is broken enough to log an error; it is just wrong in a way that surfaces three steps later as a redirect that times out or a token your API refuses because the issuer does not match.

The same server, with the four settings above, answers https://auth.example.com/realms/app.

The header matrix​

Keycloak builds its own URLs out of whatever the proxy tells it. These are measured issuer values from one Keycloak (proxy-headers=xforwarded, no hostname set) with the nginx config changed between each run:

proxy_set_header lines in nginxIssuer Keycloak publishes
(none)http://kc:8080/realms/master
Host $hosthttp://auth.example.test/realms/master
X-Forwarded-Proto $schemehttps://kc/realms/master
X-Forwarded-Host $hosthttp://auth.example.test/realms/master
Host + X-Forwarded-Protohttps://auth.example.test/realms/master
X-Forwarded-Host + X-Forwarded-Protohttps://auth.example.test/realms/master
Host + X-Forwarded-Proto + X-Forwarded-Port 8443https://auth.example.test:8443/realms/master

Three things fall out of that table. nginx sets none of these headers for you — the first row is what you get from a bare proxy_pass, and Host defaults to the upstream's name and port. Host and X-Forwarded-Host are interchangeable for the hostname, so you only need one. And X-Forwarded-Proto is the one that silently breaks everything, because TLS stops at the proxy: Keycloak sees a plaintext request and has no other way to learn the scheme. The last row is the trap in the trap — forward the internal port and it ends up in the public issuer.

Why fixing this with headers is the wrong fix​

Every row above is a URL assembled from attacker-reachable input. Set hostname to a full URL instead and the headers stop mattering. Same Keycloak, hostname=https://auth.example.test, nginx deliberately misconfigured:

nginx sendsIssuer
no headers at allhttps://auth.example.test/realms/master
X-Forwarded-Port 8443https://auth.example.test/realms/master
Host: evil.example, X-Forwarded-Host: evil.examplehttps://auth.example.test/realms/master

Pinning hostname also fixes the other famous proxy symptom. Without it, a browser request to the admin console gets:

HTTP 403
{"error":"Invalid origin"}

because Keycloak computed its origin as http://auth.example.test while the browser sent Origin: https://auth.example.test. The giveaway is that the same request from curl, which sends no Origin header, returns 200 — so "it works in curl but not in the browser" is this bug, every time. With hostname set to the full https:// URL the origin check passes.

Set hostname. Do not infer it.

So what is proxy-headers still for?​

The client IP. With hostname pinned and proxy-headers unset, Keycloak records the proxy as the client on every login event:

LOGIN_ERROR 172.18.0.3 # the nginx container

Add proxy-headers=xforwarded and the same failed login records the address that actually made the request:

LOGIN_ERROR 172.18.0.1 # the real client

Everything that reasons about "who" is making a request reads that field: the event log, the brute-force detector's per-IP lockouts, and any SIEM rule you have built on exported events. Leave proxy-headers unset and the brute-force detector sees your entire user base as one IP address.

Note the order of operations: proxy-headers is not what fixes the issuer once hostname is pinned, and hostname is not what fixes the client IP. Most guides conflate them and set only one.

The line that lets anyone forge their IP​

This is the one worth stopping on. The standard nginx recipe is:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

$proxy_add_x_forwarded_for appends the client address to whatever X-Forwarded-For the client already sent. Keycloak takes the leftmost entry. So a client that sets the header itself gets to choose. Measured, with proxy-trusted-addresses correctly set to the proxy's address:

curl -s -X POST https://auth.example.test/realms/app/protocol/openid-connect/token \
-H "X-Forwarded-For: 203.0.113.9" \
-d "client_id=admin-cli&username=nobody&password=wrong&grant_type=password"

What the backend received, and what Keycloak logged:

X-Forwarded-For: 203.0.113.9, 172.18.0.1
LOGIN_ERROR 203.0.113.9

proxy-trusted-addresses did not help, because the header did arrive from the trusted proxy — the proxy just carried the forged value in front of the real one. Change one word:

proxy_set_header X-Forwarded-For $remote_addr;
X-Forwarded-For: 172.18.0.1
LOGIN_ERROR 172.18.0.1

The Keycloak docs state the rule — the proxy "must overwrite these headers, not just append to them" — without saying what appending costs. It costs you per-IP brute-force protection and the integrity of every IP address in your audit log.

This applies to AWS Application Load Balancers as shipped. The default for routing.http.xff_header_processing.mode is append, which behaves exactly like $proxy_add_x_forwarded_for: a client-supplied X-Forwarded-For is preserved and the real address added after it. Set the attribute to remove and let the ALB write the header itself, or terminate at something you control.

How the three proxies behave by default​

Same forged request, same echo backend, no header configuration beyond routing:

ProxySets X-Forwarded-* unaskedClient's forged X-Forwarded-ForClient's forged X-Real-IP
nginx 1.29no — you write all of themkept, leftmost, if you use $proxy_add_x_forwarded_forpassed through
Caddy v2.11.9yesreplaced with the real addresspassed through
Traefik v3.8.0yesreplacedreplaced

Caddy and Traefik are safe on this point out of the box; nginx is safe only if you write the right line. All three need X-Real-IP and X-Original-URL stripped explicitly unless you are on Traefik — Keycloak does not read them, but a theme, an SPI or whatever sits behind the same proxy might.

Lock the headers to the proxy, and mean it​

proxy-trusted-addresses tells Keycloak which source addresses may set forwarded headers. The temptation is to give it the subnet. Measured, with --proxy-trusted-addresses=172.18.0.0/16 — the whole Docker network — and a forged request sent directly to Keycloak from a neighbouring container rather than through the proxy:

LOGIN_ERROR 203.0.113.9

The forgery worked, because the forger lived inside the trusted range. Narrow it to the proxy's own address and the same request records the forger:

LOGIN_ERROR 172.18.0.4 # the forger's real address, header ignored
LOGIN_ERROR 172.18.0.1 # the same bad login through the proxy

In Kubernetes, a pod CIDR is every pod in the cluster. Trust the proxy's address, or the service it fronts — not the network it happens to sit on. And keep in mind that the setting is a mitigation, not a boundary: the Keycloak docs are blunt that IP-based trust "provides weak protection" because addresses can be spoofed. If Keycloak's HTTP port is reachable from anything other than the proxy, fix that first.

A known-good nginx configuration​

Two server blocks: one for users, one for operators. This is the file that was running for the checks in the next section.

/etc/nginx/conf.d/keycloak.conf
# --- Public: end users only -------------------------------------------------
server {
listen 443 ssl;
http2 on;
server_name auth.example.com;

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

# Not for the public internet.
location /admin/ { return 404; }
location /realms/master/ { return 404; }
location /metrics { return 404; }
location /health { return 404; }

location / {
proxy_pass http://keycloak:8080;

proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr; # overwrite, never append
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port 443;

# Anything inbound that claims to be identity: drop it.
proxy_set_header X-Real-IP "";
proxy_set_header X-Original-URL "";

proxy_buffer_size 16k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 32k;
}
}

# --- Admin: operators only --------------------------------------------------
server {
listen 443 ssl;
http2 on;
server_name admin.example.com;

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

allow 10.0.0.0/8;
deny all;

location / {
proxy_pass http://keycloak:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port 443;
proxy_buffer_size 16k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 32k;
}
}

The three proxy_buffer* lines are not optional padding. nginx's default proxy_buffer_size is one 4 KB page, and Keycloak's authorization endpoint emits a response header block whose size is driven by the state your client sends — a 502 that only some clients trigger is that limit.

And the matching server side:

conf/keycloak.conf
hostname=https://auth.example.com
hostname-admin=https://admin.example.com
proxy-headers=xforwarded
proxy-trusted-addresses=10.0.1.7
http-enabled=true
health-enabled=true

When this pair is right, one warning disappears from the Keycloak startup log. When it is wrong, 26.8.2 tells you so in a single line, which is worth knowing because it is easy to scroll past:

WARNING: With HTTPS not enabled, `proxy-headers` unset, and `hostname-strict=false`, the
server is running in an insecure context. Secure contexts are required for full
functionality, including cross-origin cookies. Also if you are using a proxy, requests from
the proxy to the server will fail CORS checks with 403s because the wrong origin will be
determined. Make sure `proxy-headers` are configured properly.

Which paths to expose​

Keycloak serves the login pages, the admin console and the Admin REST API on one port. The public vhost should carry the first and none of the rest. Measured against the config above:

PathPublic hostAdmin hostWhy
/realms/app/…200200Your realms. This is the traffic.
/resources/…200200CSS, JS and images for the login page — blocking it gives you an unstyled login
/realms/master/…404200The realm your admins live in
/admin/…404200Admin console and Admin REST API
/metrics, /health404404Management port only — see below

Blocking /realms/master/ also blocks master's discovery document, which is correct: no application outside your operations team should be using the master realm.

Do not proxy port 9000. Health and metrics are served there, separately, and Keycloak's guidance is explicit that exposing it to external callers is not something you want. Two things to know about it: the port does not open at all unless you enable health or metrics (a probe against :9000 on a default server gets connection refused), and /health on the main port is a 404, not a route — a load-balancer health check pointed there will mark every backend down.

kc:9000/health/ready 200 # health-enabled=true
kc:8080/health 404 # not a route, ever

The admin console on its own hostname​

hostname-admin splits the admin surface onto a second name so you can put different network controls in front of it. With hostname=https://auth.example.test and hostname-admin=https://admin.example.test, the console's own bootstrap config comes back as:

"authUrl": "https://admin.example.test",
"authServerUrl": "https://auth.example.test"

The console authenticates against the admin hostname while the realm's issuer stays the public one — which is exactly what you want, and is why you cannot do this by rewriting URLs in the proxy. Both hostnames must reach the same Keycloak; the split is about who can get to which name, so pair it with an allow/deny block, a VPN, or a private load balancer.

Keycloak under a path​

If Keycloak has to live at https://example.com/auth rather than on its own name, the Keycloak docs list three mechanisms. They are not interchangeable, and picking the wrong pair produces a server that serves pages fine and publishes unusable URLs.

Put the path in hostname:

hostname=https://auth.example.com/auth
location /auth/ {
proxy_pass http://keycloak:8080/; # the trailing slash strips /auth
# ... the same proxy_set_header lines as above
}

Measured: the server still serves at /realms/... internally — /auth/realms/master on the container is a 404 — while every URL it publishes carries the prefix. The discovery document reports https://auth.example.test/auth/realms/master, login-page assets come back as /auth/resources/<hash>/login/keycloak.v2/css/styles.css, and the login form posts to an absolute https://auth.example.test/auth/realms/app/login-actions/authenticate.

The alternative, X-Forwarded-Prefix, is the part worth knowing:

hostnameX-Forwarded-Prefix: /authIssuer
https://auth.example.testsenthttps://auth.example.test/realms/master ❌
not set (hostname-strict=false)senthttps://auth.example.test/auth/realms/master ✅

A full hostname wins, and the prefix header is ignored. Since the whole argument above is for pinning hostname, that means: pin the path in hostname too, and treat X-Forwarded-Prefix as something to strip at the proxy rather than something to send.

Traefik and Caddy​

Both set the forwarded headers for you, so the server-side settings are the same and the proxy config is shorter.

traefik/dynamic.yml
http:
routers:
keycloak:
rule: >-
Host(`auth.example.com`) &&
!PathPrefix(`/admin`) && !PathPrefix(`/realms/master`) &&
!PathPrefix(`/metrics`) && !PathPrefix(`/health`)
entryPoints: [websecure]
service: keycloak
tls: {}
middlewares: [strip-identity-headers]
middlewares:
strip-identity-headers:
headers:
customRequestHeaders:
X-Real-IP: ""
X-Original-URL: ""
X-Forwarded-Prefix: ""
services:
keycloak:
loadBalancer:
servers:
- url: "http://keycloak:8080"
Caddyfile
https://auth.example.com {
@blocked path /realms/master/* /admin/* /metrics /health*
respond @blocked 404

reverse_proxy http://keycloak:8080 {
header_up -X-Real-IP
header_up -X-Original-URL
}
}

Traefik's path exclusions go in the router rule rather than in a middleware — there is no "deny this path" middleware, so the way to not serve a path is to not route it. On Traefik, leave forwardedHeaders.trustedIPs unset on the entrypoint unless something else sits in front of it; that default is what makes it rewrite a forged X-Forwarded-For instead of trusting it.

Verify it in five commands​

Run these from outside, against the public name, after any change to either side:

# 1. The issuer is your public URL, over https, with no port and no container name.
curl -s https://auth.example.com/realms/app/.well-known/openid-configuration | jq -r .issuer

# 2. The admin surface is not on the public name. All four must be 404.
for p in /admin/master/console/ /realms/master/ /metrics /health; do
printf '%-26s %s\n' "$p" "$(curl -s -o /dev/null -w '%{http_code}' https://auth.example.com$p)"
done

# 3. The admin console is reachable on the admin name. Expect 200.
curl -s -o /dev/null -w '%{http_code}\n' https://admin.example.com/admin/master/console/

# 4. The login page is styled — the asset path resolves, not just the HTML.
curl -s -o /dev/null -w '%{http_code}\n' "https://auth.example.com$(curl -s \
"https://auth.example.com/realms/app/protocol/openid-connect/auth?client_id=web&response_type=code&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcb&scope=openid" \
| grep -oE 'href="/[^"]*styles.css"' | head -1 | sed 's/href="//;s/"//')"

# 5. A forged client IP does not reach the event log.
curl -s -o /dev/null -X POST https://auth.example.com/realms/app/protocol/openid-connect/token \
-H "X-Forwarded-For: 203.0.113.9" \
-d "client_id=admin-cli&username=nobody&password=wrong&grant_type=password"
# then read the newest LOGIN_ERROR event: its ipAddress must be yours, not 203.0.113.9

Check 5 is the one nobody runs, and it is the one that has no symptom until you need the audit log to be true. Put all five in CI against staging and they fail on the commit that breaks them rather than during an incident.

When it is still broken​

Each of these has a different cause and a different page:

SymptomWhere it lives
502 Bad Gateway through the proxyThe proxy's error log — every cause and fix
403 Forbidden with {"error":"Invalid origin"}hostname or proxy-headers, above
Browser 403s on XHR, curl fineCORS and Web Origins, not the proxy
Login bounces between app and KeycloakCookies, not the proxy
403 on the Admin REST API with a valid tokenRoles, not the proxy

The dividing line is simple enough to apply under pressure: if the status code comes from the proxy, read the proxy's log; if it comes from Keycloak, the body will say which check failed.

Next steps​