How to Fix HTTP 502 Bad Gateway & 504 Gateway Timeout
Estimated reading time: 4 mins • Updated October 2026
A 502 Bad Gateway error occurs when one server on the internet (such as a reverse proxy, CDN, or Cloudflare edge node) receives an invalid response or no response from an inbound origin server (like Nginx, Apache, Node.js, or PHP-FPM).
Common Causes & Step-by-Step Fixes:
- Origin Web Server Crashed: Check if your PHP-FPM, Node.js, or Nginx process died due to out-of-memory (OOM). Run
systemctl status php8.2-fpmorpm2 statuson your server. - Origin Firewall Blocking CDN IPs: If using Cloudflare or a reverse proxy, ensure your server firewall (UFW / iptables) allows connections from Cloudflare IP ranges.
- DNS Misconfiguration: If you recently moved hosting providers, verify your A record points to the active server IP address.
- PHP-FPM Timeout (504): For long-running database queries or heavy scripts, increase
max_execution_timeandrequest_terminate_timeoutin your server config.