title: Reverse Proxy Bad Gateway after adding OIDC to my SPA
body: <p>im in the process of adding keycloak to my company existing SPA written in Angular.</p>
  <p>I have managed already to get the acess_token from keycloak as the initial auth works well, but when im trying to talk to my API, roughly half the requests throw a 502 bad gateway error.</p>
  <p>When I test locally everything works well and flawlessly, I am now testing it on our development kubernetes cluster.</p>
  <p>The scheme is like this :</p>
  <ul>
  <li><p>Web app (angular SPA served by an nginx server), the nginx server acts as a reverse proxy to talk to my api</p>
  </li>
  <li><p>Api server (written in nestJS, with a server side validation of my access token)</p>
  </li>
  </ul>
  <p>The NGINX server talk to the Api server using a clusterIP kubernetes service.</p>
  <p>I belive the problems comes from the token validation on the api that for some reason doesnt work sometimes or takes too long and thus throw an error.</p>
  <p>This is what it looks like :</p>
  <pre><code>@Injectable()
  export class ownKeycloakStrategy extends PassportStrategy(BearerStrategy, 'keycloak') {
    
    constructor(private readonly openidService: OpenidService, private readonly staffService:StaffService,
      private readonly adminService:AdminService) {
      
      super(
        (token, done) =&gt; this.validate(token, done)
      );
    }
  
    async validate(token, done): Promise&lt;any&gt; {
     var userInKC = null
      try {
        userInKC =  await this.openidService.validateToken(token);
      } catch (e) {
        return done(e)
      }
      // get Info from KC, if you cant means either user doesnt exist or token is not valid
      //console.log(userInKC)
      if(!userInKC) return done(new UnauthorizedException('Token not valid'))
      // complete user object with info from our db
      const userX: any = await Promise.all([
                // here searches for user in own db
                
            ]).then(x =&gt; x.find(e =&gt; e !== null));
      if (!userX) return done(new NotFoundException('User not found'))
      // here we set up the user of the rerquest so our service can work properly
      const user = {
        ...userInKC,
        _id: userX,
        status: userX.status
      }
      return done(null, user, {scope: 'all'});
    }
  }
  </code></pre>
  <p>My validateToken function :</p>
  <pre><code>public async validateToken(token) {
          try {
              const public_key = await this.public_key()
              if(public_key) {
                  const decrypt = await this.keycloak.jwt.verifyOffline(token, public_key);
                  return decrypt.content
              }
          } catch(e) {
              return this.keycloak.jwt.verify(token);
          }
      }
  </code></pre>
  <p>What i tried :</p>
  <ul>
  <li>Changing the proxy buffering of my nginx conf to allow for the bigger access_token header</li>
  <li>Try calling my api directly (i also get a bad gateway error, this time i belive from traefik (my cluster load balancer)</li>
  </ul>
  <p>Thank you for any help any of you could provide, p^lease let me know if you need to know something</p>
  <p>Thank you</p>