504 Error when using cookie


I am working on a Node App using Keycloak with Microsoft as Identity Provider.

I am facing the problem, that the login only works sometimes, and other times I receive a 504 Gateway Time-out error at the last step of the authentication, right when the identity provider should redirect to my application.

After looking at the network requests on my browser I noticed that the logins that were successful didn’t use a cookie. The ones where I get the error are sending a cookie in the headers with AUTH_SESSION_ID, AUTH_SESSION_ID_LEGACY and KC_RESTART.

Anybody can explain this behaviour?