Keycloak and oauth2-proxy: Secure an App with a Gateway
An authenticating gateway is a reverse proxy that completes the OIDC flow with Keycloak on behalf of an application that cannot do it itself, then passes the request upstream with the user's identity in a header. To put one in front of an app with oauth2-proxy:
- Create a confidential client in Keycloak whose redirect URI is
https://<public-host>/oauth2/callback. - Run oauth2-proxy with
--provider=keycloak-oidcand--oidc-issuer-url=https://<keycloak>/realms/<realm>, pointing--upstreamat the app. - Restrict who gets in with
--allowed-role, which reads Keycloak realm and client roles. - Make the app reachable only through the proxy, or the identity headers mean nothing.
- Forward the access token (
--pass-access-token) so the app can verify it independently. - Send logout through Keycloak's
end_session_endpoint, not just the proxy's cookie.
That is the whole shape. The rest of this page is the three deployment layouts, what the app actually receives, and the four ways this gets built insecurely.
Tested against
Keycloak 26.8.2, oauth2-proxy v7.15.6, nginx 1.29.8, Traefik v3.6.25, and
traefik/whoami v1.11 as the upstream application, all on one Linux host on
2026-11-30. Every command, header dump, error string and status code below is copied from
that run.