Crypto
24 Apr 2026
Read 12 min
How to fix 502 Bad Gateway error fast and permanently *
how to fix 502 Bad Gateway error and restore uptime fast with diagnostics, cache and server fixes now
What a 502 Bad Gateway really means
Your browser talks to a gateway first. That gateway can be a CDN, a load balancer, or a reverse proxy like Nginx or Apache. The gateway then calls your app server. A 502 means the gateway did not get a good answer. Common reasons include:- The app server is down, busy, or restarting.
- Timeouts are too short for slow requests.
- DNS points to the wrong origin or just changed.
- SSL or TLS handshake failed between gateway and origin.
- A firewall or WAF blocked the gateway or your IP.
- Third-party APIs your app calls are slow or failing.
Quick fixes to restore service now
Move fast, then go deeper.- Check if it is global. Use a status page or a checker to see if others get 502 too.
- Retry with a hard refresh. Many 502s are brief after deploys or restarts.
- Bypass the CDN. Point your hosts file to the origin or use the CDN’s “development mode.”
- Restart the origin stack. Restart the app, PHP-FPM, Node, Nginx/Apache, and clear any message queues.
- Roll back the last change. If the 502 started after a deploy, revert.
- Look at logs first. Error logs at the gateway and app will tell you if it is timeout, crash, or handshake.
how to fix 502 Bad Gateway error on the client side
This helps you rule out local issues and gather clean reports.- Refresh, then try Incognito or Private mode to skip bad cache.
- Clear DNS cache. On Windows, run ipconfig /flushdns. On macOS, run a DNS flush or toggle Wi‑Fi.
- Change networks. Try mobile data or another Wi‑Fi to rule out ISP or firewall blocks.
- Turn off VPN or proxy. Some VPN endpoints break TLS or filter traffic.
- Disable aggressive browser extensions that modify requests.
- If only one page 502s, clear that site’s cookies and cache.
Server and application fixes at the origin
Most 502s start here. Your goal is to restore a healthy, quick upstream response.Restart and clear bottlenecks
- Restart app workers (PHP-FPM, Node, Python, Ruby) and your web server.
- Drain long-running jobs and clear stuck queues (Redis, RabbitMQ, SQS).
- Free resources. Check CPU, RAM, disk I/O, and file descriptors.
- Kill runaway processes that hold ports or memory.
Read logs and identify the pattern
- Gateway logs (Nginx/Apache) show upstream timed out or reset.
- App logs show crashes, OOM kills, or dependency failures.
- System logs show kernel OOM or network issues.
- Note which routes fail. It is often a single heavy endpoint.
Tune your proxy and timeouts
Bad timeouts cause many 502s.- Increase upstream timeouts slightly at Nginx or Apache for slow endpoints.
- Enable keepalive with your upstream to avoid new TCP handshakes on every request.
- Raise buffer sizes if you stream larger responses.
- Ensure max connections to upstream are not too low, causing queueing.
Stabilize your app runtime
- PHP-FPM: Increase pm.max_children if workers are full; watch memory per worker.
- Node.js: Use a process manager with multiple workers; avoid blocking the event loop.
- Python/Ruby: Right-size Gunicorn/Puma workers and threads; preload app code if safe.
- Raise per-request timeouts inside the app if they are too strict.
Fix database and API slowness
- Inspect slow query logs; add needed indexes and pagination.
- Use read replicas for heavy reads; add a cache layer for hot keys.
- Set sensible timeouts and retries for third-party APIs; add circuit breakers to fail fast.
- Do not block the main request on non-critical calls; queue them for background jobs.
CDN, DNS, and load balancer checks
Your edge layer must reach the origin quickly and safely.- CDN health: If the CDN shows a 502 but origin is fine, temporarily disable the CDN route. Purge cache after fixes.
- Origin SSL/TLS: Use valid certs at origin if your CDN needs HTTPS to the origin. Match SNI and ALPN as required.
- WAF and firewall: Whitelist CDN and load balancer IPs. Disable new rules that started blocking your app.
- DNS sanity: Ensure A/AAAA records point to the right IPs. Lower TTL during migrations and raise it after.
- Load balancer health checks: Confirm the check path returns 200 fast. Do not point health checks to a heavy page.
- Stickiness: If your app needs session affinity, enable it or move sessions to a shared store.
Permanent prevention checklist
To stop future 502s, build guardrails that handle spikes and failures.- Autoscale and capacity plan: Set CPU and response time alerts. Keep headroom for traffic spikes.
- Health checks and graceful restarts: Drain connections before deploys. Use rolling or blue‑green releases.
- Circuit breakers and retries: Fail fast on bad upstreams; add low backoff retries for transient errors.
- Caching: Cache frequent GETs at CDN or app. Use Redis for sessions and hot data.
- Timeout policy: Set timeouts at each hop (client, CDN, proxy, app, DB). Keep them consistent and realistic.
- Connection reuse: Enable HTTP keepalive and HTTP/2 where possible to reduce handshake costs.
- Monitoring and alerts: Track 5xx rate, p95/p99 latency, upstream queue length, and worker counts.
- Security that does not block good traffic: Tune WAF rules, rate limit bad bots, and allowlist known gateways.
- Dependency budgets: Watch external APIs. Add fallbacks or static content when they fail.
- Disaster-ready DNS: Keep a warm standby origin or region and test failover.
When to call your host or provider
Escalate if you see any of these:- Node unreachable from the load balancer or CDN, but you cannot change the network or firewall.
- Kernel OOM kills you cannot stop due to platform limits.
- Regional outage at your cloud or CDN.
- TLS handshake failures that look like provider bugs or expired managed certs.
For more news: Click Here
FAQ
* The information provided on this website is based solely on my personal experience, research and technical knowledge. This content should not be construed as investment advice or a recommendation. Any investment decision must be made on the basis of your own independent judgement.
Contents