Contents
On modern hosting a request passes through several layers: it may start at Cloudflare, reach the web server, which hands it to PHP or to a Node.js or Python app. Each layer is a gateway to the next. If the connection breaks suddenly or the reply is malformed, the gateway answers 502 Bad Gateway. Finding which layer failed is half the fix.
What causes a 502 error
- An app that stopped or never started: a broken startup file, a missing package or a missing environment variable in a Node.js or Python app.
- A process crashing mid-request: a PHP process or app died on a fatal error or after exceeding its memory limit.
- Resource limits: the account hit its memory or process limit, so processes were ended before finishing the response.
- Cloudflare-to-origin problems: a DNS record pointing to an old server, or a firewall cutting Cloudflare’s connections.
- An invalid response from the app: malformed or oversized headers.
Fixing it as a visitor
Reload after a minute; many 502s clear once the app restarts. If it persists across devices and networks, the problem is on the site, so let the owner know.
Fixing it as the site owner
1. Find the failing layer
A Cloudflare-branded page with the error on Host means the server behind it is the problem. A Passenger page like “Web application could not be started” means the app didn’t boot; see Passenger app error.
2. Read the logs
Open cPanel, then Metrics → Errors. For Node.js and Python apps, open Software → Setup Node.js App or Setup Python App and check the log file set in the app’s settings.
3. Restart and test the app by hand
On the app’s page in cPanel click Restart. If the error remains, copy the environment command shown at the top of the app page, run it over SSH, and start the app directly to see the error:
source /home/USER/nodevenv/myapp/20/bin/activate && cd /home/USER/myapp
npm install
node app.js
Also confirm that the Startup file in the settings is the right one. The Node.js deployment guide walks through these settings.
4. Check resource usage
Open Metrics → Resource Usage and see whether the account hit its memory or process limit when the error appeared. If so, reduce the app’s usage, add caching, or move to a larger plan.
5. If the site is behind Cloudflare
Make sure the A record in Cloudflare points to the IP address shown in cPanel, then try “Pause Cloudflare on Site” for a moment. If the site works directly, the problem is between Cloudflare and the server. See using Cloudflare with your hosting.
Prevention
- Catch errors in your code (try/catch) so one bad request can’t take the whole process down.
- Watch memory usage and run heavy jobs in separate background processes.
- Test the app after every deploy, especially after updating packages.
On ArabyHost
On AI/Developer plans, Node.js 20 and 24 and Python 3.9-3.12 apps run through the CloudLinux selectors and Phusion Passenger, with SSH jailed in CageFS for reading errors and starting the app by hand, and background workers kept alive with cron. Every account has its own LVE resource limits, and LiteSpeed is the web server. Ask the Araby AI assistant to read your error log and explain why the app stopped.
FAQ
What's the difference between 502 and 504?
With 502, the gateway got a bad response or the connection broke off. With 504, no response arrived within the timeout. The first usually points to a crash, the second to slowness.
How do I tell whether the 502 comes from Cloudflare or my host?
Cloudflare's page shows three icons, Browser, Cloudflare and Host, with the error marked on the part at fault. A plain page without Cloudflare branding comes from your hosting server.
My Node.js app shows 502 after deploying. Where do I start?
Start the app by hand over SSH inside its environment and read the startup error. It's usually a missing package, a missing environment variable or the wrong startup file.