Crypto
24 Sep 2026
Read 13 min
How to fix 502 Bad Gateway and restore your site *
How to fix 502 Bad Gateway and resolve the download error to restore site access quickly and safely
What a 502 error tells you
Where the problem sits
A 502 does not always mean your app is down. It means the gateway cannot get a good answer from the upstream. The break can be:- At the origin app (crashed, slow, or blocked)
- At the proxy or CDN (timeouts, filters, wrong DNS)
- In the network path (firewalls, routing, TLS mismatch)
How it differs from 504
A 504 is a timeout. A 502 is a bad or no response. Both feel the same to users, but you fix them in different ways. With 502, you often check misconfig, SSL, or upstream crashes first.How to fix 502 Bad Gateway: quick wins
Client-side steps
Before you touch servers, rule out local issues:- Reload the page and try a different browser
- Open a private/incognito window to bypass cache
- Clear your browser cache and cookies for the site
- Switch networks (Wi‑Fi to mobile) to avoid ISP hiccups
- Flush DNS (Windows: ipconfig /flushdns, macOS: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder, Linux: sudo systemd-resolve –flush-caches)
- Set DNS to 1.1.1.1 or 8.8.8.8 and retest
Check site status from outside
Use an external checker or uptime monitor. If it fails worldwide, move on to server fixes. If it fails only in one region or ISP, suspect DNS or CDN.Confirm your origin is up
Ping the app directly
Bypass the proxy if you can:- Use the server’s internal IP or hosts-file entry to hit the app directly
- Call the app port (e.g., http://server:8080 or http://127.0.0.1) from the server
- Use curl -I or curl -v to see headers and errors
Review server health
- CPU, RAM, and disk: free up space and memory
- Process status: ensure PHP-FPM, Node.js, Python (uWSGI/Gunicorn), or Ruby (Puma/Unicorn) is running
- Database: confirm it accepts connections
- Firewall: allow traffic from your CDN or load balancer IPs
Fix the proxy and web server layer
Nginx as a reverse proxy
Common issues:- Timeouts too low: raise proxy_read_timeout, proxy_connect_timeout, and proxy_send_timeout
- Buffer limits: increase proxy_buffers and proxy_busy_buffers_size for large headers
- Upstream mismatch: verify upstream names and ports in upstream blocks
- FastCGI/PHP-FPM: align fastcgi_read_timeout and confirm PHP-FPM is not at max_children
Apache and gateway timeouts
- Check ProxyTimeout and Timeout directives
- Confirm mod_proxy_fastcgi or mod_proxy_fcgi targets the right socket/port
- Raise LimitRequestFieldSize if headers are large
Load balancers
- Set health checks to match a fast, 200-OK endpoint
- Enable sticky sessions (if your app needs session affinity)
- Remove failing instances and redeploy them cleanly
CDN and DNS problems
Cloudflare, Fastly, and others
If you use a CDN and wonder how to fix 502 Bad Gateway there, try this:- Pause or bypass the CDN (e.g., “gray cloud” in Cloudflare) to test origin directly
- Update DNS A/AAAA records to the correct origin IP
- Match SSL mode: Full (strict) needs a valid origin cert; Flexible often breaks redirects and apps
- Whitelist CDN IP ranges on your origin firewall and fail2ban
- Disable “Under Attack” or bot filters briefly to rule out false positives
Propagating DNS
If you just changed hosts:- Expect global DNS to take time (TTL-based)
- Lower TTL before big moves in the future
- Keep old and new origins in sync during cutover
Application-level fixes
Crashes and slow startups
- Check app logs for unhandled exceptions on boot
- Warm up caches and precompile assets on deploy
- Move heavy jobs to background workers; do not block web threads
- Increase concurrency (PHP-FPM pm.max_children, Node cluster workers, Gunicorn workers)
Large headers and cookies
Too many cookies or huge headers can trigger 502s:- Reduce cookie size and count; avoid storing JWTs in cookies if large
- Raise header limits on Nginx/Apache if needed
- Compress and paginate responses instead of sending giant payloads
TLS and cipher mismatch
- Ensure the origin supports modern TLS and ciphers your proxy expects
- Update OpenSSL and web server packages
- Renew expired certs and fix full chain (intermediates)
Platform-specific steps
WordPress
If you need a fast path for how to fix 502 Bad Gateway in WordPress:- Disable plugins by renaming wp-content/plugins via SFTP; if site returns, re-enable one by one
- Switch to a default theme (rename your active theme folder)
- Regenerate .htaccess (Settings → Permalinks → Save) or restore a known good one
- Increase PHP memory limit and max_execution_time
- Check PHP-FPM error logs; raise pm.max_children if pool is saturated
- Update PHP version to a supported release
Node.js
- Ensure the process manager (PM2/systemd) restarts on crash
- Listen on the right port and health path
- Use a reverse proxy (Nginx) for keep-alive and buffering
- Profile slow endpoints; do not block the event loop
Python (Django/Flask)
- Use Gunicorn/uWSGI with enough workers and threads
- Set proper timeouts and graceful timeouts
- Serve static/media via CDN or Nginx, not the app
Docker and Kubernetes
- Set readiness/liveness probes to a fast, reliable path
- Match Ingress and service timeouts with app behavior
- Scale replicas during traffic spikes
- Use resource requests/limits to avoid throttling and OOM kills
Logs that point to the cause
Where to look
- Nginx: error.log and access.log with upstream status
- Apache: error_log with proxy module messages
- CDN dashboards: origin reachability, 502/504 charts
- App logs: exceptions and startup failures
- System logs: OOM killer, kernel, firewall
Prevent the next outage
Resilience patterns
- Add health checks, circuit breakers, and retries with backoff
- Cache common pages at the CDN and use stale-while-revalidate
- Use a status page and incident playbooks
- Autoscale based on CPU, queue length, or request rate
- Run synthetic checks from many regions and alert fast
- Test failover paths and keep hot standbys ready
Release safer
- Blue/green or canary deploys to limit blast radius
- Feature flags to turn off risky code without redeploy
- Pre-deploy smoke tests and load tests
(Source: https://www.nytimes.com/2026/09/23/us/politics/ai-leaders-united-nations-global-response.html)
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