NodeJs application behind Amazon ELB throws 502 NodeJs application behind Amazon ELB throws 502 express express

NodeJs application behind Amazon ELB throws 502


Try setting server.keepAliveTimeout to something other than the default 5s. See: https://nodejs.org/api/http.html#http_server_keepalivetimeout. Per AWS docs, you'd want this to be greater than the load balancer's idle timeout.

Note: this was added in Node v8.0.0

Also, if you're still on the Classic ELB, consider moving to the new Application Load Balancer as based on current experience this seems to have improved things for us a lot. You'll also save a few bucks if you have a lot of separate ELBs for each service. The downside could be that there's 1 point of failure for all your services. But in AWS we trust :)


In my case, I needed upgrade nodejs version:

https://github.com/nodejs/node/issues/27363

After that the problem was fixed.


Change your server.listen() to this:

const port = process.env.PORT || 3000;const server = app.listen(port, function() {  console.log("Server running at http://127.0.0.1:" + port + "/");});

You can read more about this here.