title: AWS Load Balancer 502 Bad Gateway
body: <p>I have microservices written in node/express hosted on EC2 with an application load balancer.</p>
  <p>Some users are getting a 502 even before the request reaches the server.</p>
  <p>I register every log inside each instance, and I don't have the logs of those requests, I have the request immediately before the 502, and the requests right after the 502, that's why I am assuming that the request never reaches the servers. Most users solve this by refreshing the page or using an anonymous tab, which makes the connection to a different machine (we have 6).</p>
  <p>I can tell from the load balancer logs that the load balancer responds almost immediately to the request with 502. I guess that this could be a TCP RST.</p>
  <p>I had a similar problem a long time ago, and I had to add <code>keepAliveTimeout</code> and <code>headersTimeout</code> to the node configuration. Here are my settings (still using the LB default of the 60s):</p>
  <pre><code>server.keepAliveTimeout = 65000;
  server.headersTimeout = 80000;
  </code></pre>
  <p>The metrics, especially memory and CPU usage of all instances are fine.</p>
  <p>These 502 errors started after an update we made where we introduced several packages, for instance, axios. At first, I thought it could be axios, because the keep-alive is not enabled by default. But it didn't work. Other than the <a href="https://github.com/axios/axios" rel="nofollow noreferrer">axios</a>, we just use the <a href="https://github.com/request/request" rel="nofollow noreferrer">request</a>.</p>
  <p>Any tips on how should I debug/fix this issue?</p>