A 502 Bad Gateway error generally means the web server or application proxy couldn't get a valid response from your Node.js application.
If the problem appears after several hours, it may indicate an application process crash, resource exhaustion, or an unhandled error.
Here are some important things to investigate:
- Memory usage: Check whether your application gradually consumes more memory.
- Unhandled errors: Look for uncaught exceptions and unhandled promise rejections.
- Resource limits: Check whether your hosting account is reaching its CPU, memory, or process limits.
- Long-running requests: Investigate database queries or API requests that take too long.
- Application logs: Check the exact time the 502 error occurs and compare it with the Node.js logs.
You can also monitor memory usage from within your application:
setInterval(() => {
const memory = process.memoryUsage();
console.log({
rss: memory.rss,
heapUsed: memory.heapUsed,
heapTotal: memory.heapTotal
});
}, 60000);
This logs memory usage every minute and can help identify potential memory leaks.
If the application consistently fails after a similar period, investigate the underlying cause instead of relying on repeated restarts.