admin管理员组

文章数量:1123274

I'm working on a Next.js 14 app with Next-Auth configured to use Keycloak as the provider. The entire setup is running in containers managed via Portainer, with the production environment behind a Caddy web server.

While everything works perfectly in my local environment, I'm encountering an issue in production where requests get stuck in a 302 redirect loop during CSRF validation.

Here are the relevant logs:

GET /api/auth/csrf 200 in 14ms POST /api/auth/signin/keycloak 200 in 13ms POST /api/auth/signin/keycloak 200 in 16ms GET /api/auth/signin?csrf=true 200 in 11ms GET /api/auth/signin?csrf=true 200 in 30ms POST /api/auth/signin/keycloak 302 in 37ms GET /api/auth/signin?csrf=true 200 in 23ms

It seems to be continuously redirecting on the CSRF endpoint after a 302.

Local Environment: Works fine, no issues.
Production Environment: Running in containers via Portainer, behind a Caddy web server.
Keycloak and Next-Auth Configuration: Standard setup following the Next-Auth documentation for Keycloak.

I'm wondering if this could be related to:

Cookie handling or CSRF token validation in the production setup.
Caddy server configuration (e.g., headers, proxy settings).
Something specific about running everything in containers under Portainer.

Has anyone encountered a similar issue or have suggestions on how to resolve this? Any pointers or guidance would be greatly appreciated.

this is the caddyconf file

keycloak.mydomain {
    reverse_proxy keycloak-dev:8080 {
        header_up Host {host}
        header_up X-Real-IP {remote}
        header_up X-Forwarded-For {host}
#        header_up X-Forwarded-Proto {scheme}
#        header_up X-Forwarded-Host {host}
        header_up Forwarded "for={remote};proto={scheme};host={host}"
        header_up Upgrade {>Upgrade}
        header_up Connection {>Connection}
    }
    log {
        output stdout
        level DEBUG
    }
}

dashboard.mydomain {
    reverse_proxy dashboard-ui-dev:3001 {
        header_up Host {host}
        header_up X-Real-IP {remote}
        header_up Cookie {>Cookie}
        header_up Authorization {>Authorization}
        header_up CSRF-Token {>CSRF-Token}
        header_up X-CSRF-Token {>X-CSRF-Token}
        header_up Upgrade {>Upgrade}
        header_up Connection {>Connection}
    }
    log {
        output stdout
        level DEBUG
    }
}

本文标签: nextjsCaddyKeycloak blank page in redirect loginStack Overflow