NextAuth with Nginx proxy: Invalid grant code error when using proxied URL #12878
Unanswered
Guruprasad16
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Environment
Next.js running as a container
NextAuth.js
Nginx as reverse proxy running as a container
Keycloak as OAuth provider running on the cloud
Container setup with Docker
Issue Description
I'm facing an issue with NextAuth.js when my Next.js application is running behind an Nginx reverse proxy. The authentication flow works fine when accessing the application directly through its internal port, but fails when accessing it through the Nginx proxy.
What works:
When setting NEXTAUTH_URL=http://localhost:3000/ekeweb/api/auth and accessing the app directly, authentication works fine (but redirects to port 3000).
What doesn't work:
When setting NEXTAUTH_URL=http://localhost/ekeweb/api/auth and accessing through Nginx proxy, I get an invalid_grant (Code not valid) error.
Error logs:
2025-04-16 10:00:46 name: 'OAuthCallbackError',
2025-04-16 10:00:46 code: undefined
2025-04-16 10:00:46 providerId: 'keycloak',
2025-04-16 10:00:46 message: 'invalid_grant (Code not valid)'
Questions:
Any guidance would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions