ExpressJS - Elastic Beanstalk 502 Bad Gateway
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A 502 Bad Gateway on AWS Elastic Beanstalk usually means the reverse proxy could not get a valid response from your Node or Express application. In practice, the most common causes are that the app never started correctly, crashed immediately, or is listening on the wrong port.
What is actually failing
On Elastic Beanstalk, a proxy such as Nginx typically sits in front of the Node process. The browser talks to the proxy, and the proxy forwards requests to your app. If the app is down, unreachable, or returning nothing useful, the proxy emits the 502.
That means the error page is often just a symptom. The root cause is usually inside the application process or its startup configuration.
First thing to verify: the port
An Express app on Beanstalk should listen on the port provided by the environment. Hard-coding a local development port is a classic cause of 502 errors after deployment.
If the app listens on the wrong port, the reverse proxy cannot reach it even if the process itself is alive.
Check whether the app is crashing
The next common problem is startup failure. Missing environment variables, bad imports, a syntax error, or a failed database connection can crash the app before it ever serves traffic.
That is why Beanstalk logs matter. The correct debugging move is usually to inspect application logs and startup logs, not to stare at the 502 page itself.
Typical clues include:
- '
npm startfailing' - module import errors
- runtime exceptions during boot
- database connection timeouts
- missing environment variables
If the process never stabilizes, the proxy has nothing healthy to forward requests to.
Make startup explicit
Elastic Beanstalk needs to know how to launch the application. That usually means your package.json must have a working start script.
If the start script is wrong, references the wrong file, or depends on missing build steps, the deployment may look superficially successful while the application process is actually dead.
Health checks and environment assumptions
A 502 can also appear when the app technically starts but is too slow, blocked on an external dependency, or returns errors for the health-check path. If your app waits forever for a database before binding the port, Beanstalk can mark it unhealthy even though the code itself is syntactically fine.
That is why it is often safer to start the web server promptly and handle dependency failures explicitly rather than blocking the entire process forever during boot.
Common Pitfalls
- Hard-coding the local development port instead of using
process.env.PORT. - Missing or broken
npm startconfiguration. - Application crashes during startup due to missing packages or environment variables.
- Waiting on external services before the server binds the port.
- Treating the 502 page as the root problem instead of checking the app and platform logs.
Summary
- On Elastic Beanstalk, a 502 usually means the proxy could not talk to your Express app successfully.
- The most common causes are wrong port binding, startup failure, and crashing application code.
- Always listen on
process.env.PORT. - Make sure your
package.jsonstart script actually launches the deployed server. - Use Beanstalk logs to find the real failure instead of debugging only from the browser error page.

