The Simplest Way to Make Keycloak and Nginx Work Like They Should
Running keycloak and nginx together pairs an identity tool with a fast web server. Nginx routes incoming web traffic while Keycloak checks user logins and issues tokens. Teams use this setup to protect internal web apps without rewriting app logic. Users log in once through Keycloak and reach backend services behind Nginx. How keycloak and nginx handle logins and traffic The login flow starts when a user opens an app behind Nginx. Nginx sees that the user has no active session cookie yet. It
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
Running keycloak and nginx together pairs an identity tool with a fast web server. Nginx routes incoming web traffic while Keycloak checks user logins and issues tokens.
Teams use this setup to protect internal web apps without rewriting app logic. Users log in once through Keycloak and reach backend services behind Nginx.
How keycloak and nginx handle logins and traffic
The login flow starts when a user opens an app behind Nginx. Nginx sees that the user has no active session cookie yet. It sends the browser over to the Keycloak login page right away.
Keycloak asks the user for a password and a second factor. It checks those details against its user store or a company directory. Once the check passes, Keycloak creates a signed token with user roles.
The browser carries this signed token back to the Nginx web server. Nginx checks that the token is valid before passing the request forward. A small helper module often sits next to Nginx for this check.
After the check passes, Nginx reads the user email and group roles. It writes those details into plain request headers for the backend app. The backend app reads the headers to know who made the call.
Keycloak also sits behind Nginx in many production setups for safety. Nginx handles public TLS certificates and passes login traffic to Keycloak. This keeps the identity server off the raw public internet at all times.
Common failure modes and how to scope roles
Header spoofing is a common flaw when teams set up both tools. If Nginx does not clear incoming headers, an attacker can send fake roles. Nginx must wipe all identity headers from outside requests before adding new ones.
Shared client secrets and broad tokens also create big risks for teams. When every Nginx server uses one client profile, one leak breaks all apps. A stolen token from one small app can open the whole network.
Keycloak + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Long token lifetimes make this token risk much worse for your team. If a token lasts all day, taking away a role takes hours. Nginx will keep letting the user in until the token expires.
Network paths between Nginx, Keycloak, and backend apps also need tight rules. If a user can reach the backend app port, they can skip Nginx. Firewall rules must force all app traffic through the Nginx server first.
Unlogged requests and database calls leave blind spots during a security review. Nginx logs show web paths, and Keycloak logs show when users log in. Neither tool logs the exact queries or sensitive data inside backend sessions.
Scoping permissions and network paths in production
Start by creating a separate Keycloak client for each Nginx app route. Give each client only the user groups that specific app needs to read. This stops a token from working on a different web app.
Keep access tokens short so role changes take effect within a few minutes. Let the auth helper handle token refresh checks in the background for users. Drop any session right away when Keycloak says the user is locked out.
Lock down network routes so only Nginx can talk to your backend apps. Put Keycloak management pages on a private network port for admins only. Do not expose the Keycloak admin console on your public Nginx listener.
When backend apps return sensitive data, add data masking rules for your team. Masking rewrites matched fields like emails, phone numbers, card numbers, or national IDs. This keeps private user records safe during daily support work.
Check database and API queries on the wire
Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.
Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.
Frequently asked questions
Can Nginx validate Keycloak tokens on its own?
Open-source Nginx needs an extra helper module to check OpenID login tokens. Many teams run an auth subrequest service next to Nginx for this step. Nginx asks the helper to check the token on every web request.
Why do login loops happen between Nginx and Keycloak?
Login loops happen when forwarded headers do not match the public web address. Nginx must send the real host and protocol headers back to Keycloak. Without them, Keycloak builds redirect links for plain HTTP instead of HTTPS.
How should we protect the Keycloak admin console behind Nginx?
Never expose the Keycloak admin path on your public Nginx server block. Serve the admin console only on an internal port for your admin network. This blocks outside attackers from guessing admin passwords on the public web.
Related guides
In this post