502 Bad Gateway: causes and how to fix it
· 6 min read
What the error means
A 502 is an answer from a server that is working. The web server in front (nginx, Apache, a load balancer or a CDN such as Cloudflare) received the request, passed it on to the application behind it (PHP-FPM, a Node.js or Python process, a backend server) and got no usable answer back: the connection was refused, dropped or returned garbage. "Gateway" is the server in front; "bad" is what came back from behind.
So a 502 tells you two things at once: the front is up, and the back is down or broken. That narrows the search considerably.
Common causes
- The application process is not running. PHP-FPM crashed or was stopped, the Node.js process died, the container exited. The front cannot connect at all.
- The application is overloaded. All PHP-FPM workers are busy, the queue is full, new connections are refused. Often after a traffic spike or a slow database query that blocks every worker.
- A wrong socket or port in the configuration. After an update or a PHP version change, nginx still points at
php8.2-fpm.sockwhile the new pool listens onphp8.4-fpm.sock. - Timeouts set too tight. The backend answers, but later than the front is willing to wait. Some setups report this as 502, others as 504 Gateway Timeout.
- The backend sends an oversized header or an invalid response. Typical for frameworks that put a lot into cookies; nginx then logs "upstream sent too big header".
- The CDN cannot reach your server. With Cloudflare in front, a 502 page with the Cloudflare logo means Cloudflare got nothing from your origin: firewall, wrong IP after a move, or the server is simply down.
If you are a visitor
You cannot fix a 502 on someone else's site. Reload once after a minute; if it persists, the site's owner has to act. For your own site, go on reading.
If it is your site: check first
Is it down for everyone? Run the website down checker. It shows the status code from outside and whether a CDN or the origin answered.
Shared hosting or site builder? A 502 there is almost always the host's problem (an overloaded PHP pool on a shared node). Skip to the message for your host below.
Your own server: log in by SSH and ask the web server what went wrong.
# nginx: the last errors, newest at the bottom
sudo tail -n 50 /var/log/nginx/error.log
# Apache (Debian/Ubuntu)
sudo tail -n 50 /var/log/apache2/error.log
Typical lines and what they mean:
connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory): PHP-FPM is not running, or the socket path is wrong.connect() failed (111: Connection refused) while connecting to upstream: the backend port is not listening.upstream prematurely closed connection: the backend process crashed while answering, often a PHP fatal error or out-of-memory.upstream sent too big header: increase the buffers (see below).(11: Resource temporarily unavailable): all PHP-FPM workers are busy.
Fixes on your own server
1. Start the stopped service
# find the PHP-FPM version you use
systemctl list-units --type=service | grep -E 'php|fpm'
systemctl status php8.3-fpm
sudo systemctl start php8.3-fpm
# Node.js or Python apps managed by systemd
sudo systemctl restart myapp
If the service refuses to start, the reason is in journalctl -u php8.3-fpm -n 50. The most common one is a full disk (df -h), the second a syntax error in a pool configuration (sudo php-fpm8.3 -t).
2. Fix the socket or port
Compare what nginx connects to with what PHP-FPM listens on:
grep -r fastcgi_pass /etc/nginx/sites-enabled/
grep -r '^listen' /etc/php/*/fpm/pool.d/
The two must match. After a change, always test the configuration before reloading:
sudo nginx -t && sudo systemctl reload nginx
With Apache and PHP-FPM via mod_proxy_fcgi, the same check applies to the SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost" line; test with sudo apachectl configtest.
3. Give PHP-FPM room
If the error log says workers are busy or server reached pm.max_children, raise the limit in the pool file (/etc/php/8.3/fpm/pool.d/www.conf):
pm = dynamic
pm.max_children = 20
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
Each worker needs memory (often 30 to 80 MB for WordPress); check free -m before you raise the number, and look for the slow query or plugin that keeps the workers busy in the first place.
4. Raise the buffers for big headers
In the nginx server block:
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffers 8 16k;
proxy_buffer_size 32k;
5. WordPress specifics
A 502 right after a plugin or theme update usually means PHP is running out of memory or hitting a fatal error on every request. Rename the plugin folder over SFTP or SSH to switch it off (wp-content/plugins/plugin-name to plugin-name.off), then reload the site. Raise memory_limit in the pool or php.ini if journalctl shows "Allowed memory size exhausted". If you use the WP-CLI: wp plugin deactivate plugin-name.
6. Behind Cloudflare
Check that the origin answers directly: curl -I https://YOUR-SERVER-IP -H 'Host: example.com' -k. If that works, the firewall on the server may block Cloudflare's address ranges, or the DNS record in Cloudflare points at an old IP.
When it is your host's problem
On shared hosting, you cannot restart PHP-FPM. Send your host this, with the time and the exact error:
Hello, my website example.com returns "502 Bad Gateway" since about 14:30 UTC. An outside check confirms it; I have not changed anything on my side. Could you please check the PHP processes and the web server on my hosting account and tell me when it is back?
Preventing the next one
A 502 is nearly always a stopped or overloaded backend, and both are visible before visitors notice: a PHP-FPM service that stopped, memory running out, a disk filling up. Server monitoring with the agent reports exactly these from inside the server, and website checks from outside catch the 502 the moment it appears. On your own server, a quick fix can restart the stopped service after its configuration test passes; if the cause is deeper, a deep fix takes over around the clock.
What now
Check it from outside
Is the site down for everyone or just for you, and what does the server answer? Free, no sign-up.
Website down checkerDown? We step in.
On your own server or in a webspace with SSH access: a quick fix at a fixed price for the common causes, a deep fix billed in 15-minute blocks for the rest, around the clock. Price before you order.
Get it fixedMore guides
- Your website is down. Here is what to do in the first 15 minutes.
- Server down: what to check first, and in which order
- 503 Service Unavailable: overloaded or in maintenance?
- 504 Gateway Timeout: what it means and how to fix it
- DNS_PROBE_FINISHED_NXDOMAIN: causes and fixes
- ERR_CONNECTION_REFUSED: causes and how to fix it
- "Your connection is not private": the SSL certificate warning