How to fix the 502 Bad Gateway error in WordPress
A 502 means one server passed the request to another and got no usable answer. On a WordPress site that is usually nginx, or a CDN, failing to get a page from PHP. Your content is safe. Most fixes belong to whoever runs the server, and a few checks tell you what to ask for.
- By
- WP Ministry
- Published
- Tested on
- WordPress 7.1.3, PHP 8.3.35
In short
- A 502 is written by the server in front of WordPress, not by WordPress. Your posts, settings and uploads are not touched.
- Reload after a minute, then try from a second network. A 502 from a restart of PHP clears by itself.
- If it began with a plugin you installed or updated, switch that plugin off from outside the dashboard.
- On shared or managed hosting the rest is the host's. Send the exact time, the address and what changed.
- On a server you run yourself, one line in nginx's error log says why it answered 502.
"502 Bad Gateway" comes from a server that stands in front of another. It passed the visitor's request on and got back nothing it could use. On a WordPress site the server in front is usually nginx, a load balancer or a CDN, and the one behind is PHP, which builds the page. It differs from a 503, where the server itself is not ready to take requests, usually through overload or maintenance, and from a 504, where the server in front waited and no answer came in time.
Nothing has been lost. WordPress did not write this page, and often never ran. Your posts, settings and uploads are as they were. The one thing to check afterwards is whatever was being saved at that moment, such as an order or a post you were editing.
Who can fix what
MDN's reference says that fixing a 502 probably takes investigation by the server's owners or administrators. On shared or managed hosting that is the host: you cannot see nginx or PHP-FPM, let alone restart them. What you can do is in the first three fixes and the last. The two fixes marked "with root" are for a server you run yourself, such as a VPS.
Where it goes wrong
A page request passes through each of these in turn. This one comes from the web server.
- Browser
- DNS
- HTTPS
- CDN or firewall
- Web server (this error comes from here)
- PHP
- WordPress
- Database and files
What causes it
PHP-FPM is stopped or restarting
CommonOn an nginx server PHP runs as a service of its own, PHP-FPM. While it is stopped, and for the moment a restart takes, nginx has nothing to connect to and answers 502.
Fix: Reload after a minute, then try a second network, or Send the host what it needs to find the cause, or With root: read the logs, then restart PHP-FPM
A PHP process ended partway through a request
Commonnginx handed the request over, and the process closed the connection without answering. Code in a plugin or theme crashed PHP itself, or the server ended the process. PHP-FPM kills a worker that runs past its own time limit, and Linux ends processes when memory runs out.
Fix: Switch off the plugin that ends PHP's process, or Send the host what it needs to find the cause, or With root: read the logs, then restart PHP-FPM
Every PHP worker is busy
SometimesPHP-FPM serves a set number of requests at once. In a traffic spike, or when slow requests hold every worker, new requests wait in a queue. Pages slow down first. A 502 follows once the queue is full and connections are turned away.
Fix: Reload after a minute, then try a second network, or Send the host what it needs to find the cause, or With root: read the logs, then restart PHP-FPM
A CDN or proxy in front got no proper answer from your server
SometimesWith a service such as Cloudflare in front, the 502 is either its report of what your server answered or a fault of its own. The error page shows which.
nginx is pointed at the wrong place for PHP
SometimesThe server block names a socket or an address for PHP, and PHP-FPM listens at another. Every PHP page answers 502 until the two match. It follows a change on the server, such as a new server block.
Fix: With root: point nginx at the socket PHP-FPM listens on
How to fix it
Reload after a minute, then try a second network
- Easy
- No risk
- About 5 minutes
Step 1: Wait a minute and reload
A restart of PHP is brief, and a busy spell passes. If the page loads, write down the time. One 502 that clears needs nothing more. One that keeps returning is worth a message to the host.
Step 2: Open the site from a second network
Use a phone on mobile data with Wi-Fi off. If the site loads there, the fault is on your side. MDN names this as the exception to "it is the server": when the site works for other visitors, check your own network settings, such as a VPN, a proxy, a firewall or DNS.
Step 3: Look at the status pages
Open the host's status page, and the CDN's if you use one. If either reports an incident, wait for it to end before you change anything on the site.
Not sure this is a 502 at all, or what kind of outage you have? The runbook for a WordPress site that is down sorts an outage by what the visitor sees.
Switch off the plugin that ends PHP's process
- Takes care
- Low risk
- About 10 minutes
- Steps tested on WordPress 7.1.3
Take this fix when the 502 began right after you installed or updated a plugin, or shows only on pages that plugin has a hand in. The dashboard may answer 502 as well, so the plugin is switched off from outside it.
Step 1: Rename the plugin's folder
In your host's file manager or over SFTP, open
wp-content/plugins/and add-offto the name of the plugin's folder. WordPress can no longer find the plugin and switches it off. Its settings are kept.Step 2: Reload the page that failed
If it loads, that plugin was the cause. Leave it off, then install the version you had before or tell its author.
Step 3: If nothing changed, put the folder back
Rename it to what it was. The plugin was not the cause, and the next fix is the one to take.
With WP-CLI, give the plugin's folder name. The two extra options stop WP-CLI from loading plugins and the theme, so the faulty code cannot end the command as well:
wp plugin deactivate plugin-folder-name --skip-plugins --skip-themesIf you cannot tell which plugin it is, how to deactivate plugins when you are locked out covers switching all of them off and bringing them back one at a time.
To undo it: Rename the folder back, or activate the plugin again.
Send the host what it needs to find the cause
- Easy
- No risk
- About 15 minutes
On shared and managed hosting this is the fix for everything else on this page. Support staff can read the logs you cannot.
Step 1: Write down the facts
- The exact time of a 502, with your time zone.
- The full address that failed, and whether every page fails or only some.
- The words on the error page, exactly.
- What changed last: an update, a new plugin, or a peak in visitors such as a sale or a newsletter.
Step 2: Ask questions that have an answer
text1. Was PHP-FPM running for my site at that time, and was it restarted? 2. What does nginx's error log say for that request? 3. Did my PHP pool reach pm.max_children? 4. Was a PHP process killed, for memory or for running too long?Step 3: Act on the answer
A pool that reaches its limit in ordinary traffic means slow requests or a plan that is too small. How to tell a hosting fault from a site fault has a full message to copy, and the signs that the plan itself is the problem.
With root: read the logs, then restart PHP-FPM
- Advanced
- Low risk
- About 20 minutes
These commands are for a Linux server that uses systemd. The file paths are the ones Debian's packages use, shown for PHP 8.4. Put your own PHP version in its place. Other distributions keep these files elsewhere.
Step 1: See whether PHP-FPM is running
The first command lists the PHP services by name, stopped ones included. The second says whether one is running and shows its most recent log lines.
bashsystemctl list-units --all --type=service 'php*' sudo systemctl status php8.4-fpmStep 2: Read nginx's error log at the time of a 502
nginx's guide gives two places for it:
/var/log/nginxand/usr/local/nginx/logs.The line says What it means connect() to … failed, endingwhile connecting to upstreamnginx could not open a connection to PHP. PHP-FPM is stopped, listens somewhere else (the next fix), or has a full queue. upstream prematurely closed connectionPHP took the request, and its process ended before it answered. upstream sent too big headerThe response's header did not fit in fastcgi_buffer_size, so nginx treats the response as invalid. Raise that setting.upstream timed outThat request was answered 504, not 502. PHP was slow, not gone. bashsudo tail -n 50 /var/log/nginx/error.logStep 3: Read PHP-FPM's log for the same minute
Its place is the
error_logline ofphp-fpm.conf.A line with
server reached pm.max_children settingmeans every worker was busy:pm.max_childrenis the limit on requests served at once. A line withexecution timed outandterminatingmeans a worker ran pastrequest_terminate_timeoutand was killed. A child thatexited on signalwas ended from outside or crashed.bashgrep -n "^error_log" /etc/php/8.4/fpm/php-fpm.confStep 4: Restart PHP-FPM
Reload the site. A restart clears a stuck service and says nothing about why it stuck. If the 502 returns, what the logs named is the thing to fix.
bashsudo systemctl restart php8.4-fpm
With root: point nginx at the socket PHP-FPM listens on
- Advanced
- Back up first
- About 15 minutes
Step 1: See where nginx sends PHP
bashsudo nginx -T | grep fastcgi_passStep 2: See where PHP-FPM listens
The pool file has a line beginning
listen =.bashgrep -n "^listen" /etc/php/8.4/fpm/pool.d/www.confStep 3: Make the two match
Edit the
fastcgi_passline in the server block. A socket is written withunix:before its path. An address is written as a host and a port, such as127.0.0.1:9000.nginxfastcgi_pass unix:/the/path/on/the/listen/line;Step 4: Test, then reload
bashsudo nginx -t && sudo nginx -s reload
Writing a server block from nothing? Answer a few questions in the nginx server block generator and get one for a WordPress site, built on the rules in WordPress's own nginx documentation.
To undo it: Put the old fastcgi_pass line back, test the configuration and reload nginx.
With a CDN in front: find out which side is failing
- Easy
- No risk
- About 10 minutes
This is what Cloudflare's documentation says of its own 502 pages. Another CDN's will differ, so read its documentation the same way.
Step 1: Read the error page
A 502 page with Cloudflare's branding means your server answered 502 and Cloudflare passed that on. Cloudflare calls this the most common case. A blank 502 page without the branding comes from Cloudflare itself.
Step 2: If your server answered it, go back to the fixes above
Cloudflare's advice is to contact your hosting provider with the error code and message, the time with its time zone, and the address that failed. It says the same for an error page that does not mention Cloudflare at all.
Step 3: If Cloudflare answered it, write to Cloudflare
Its support asks for the time with its time zone, the address that answered 502, and what your browser shows at
/cdn-cgi/traceon your domain.
Cloudflare also has codes of its own, 520 to 526, for other failures to reach a server. 521, for one, means the server refused Cloudflare's connections. Its documentation has a page for each.
When to get help
Stop when the 502 has lasted more than a few minutes, the plugin you last changed is off, and the host says the server is healthy. The cause is then in logs you cannot see. On a server you run yourself, stop when the logs name nothing you recognize. Restarting PHP again and again clears the symptom and leaves the cause in place.
Common questions
Is a 502 my fault or my host's?
Most often it is on the server's side, which on shared or managed hosting means the host's. There are two exceptions you can settle yourself: your own network, and a plugin that ends PHP's process. The first two fixes cover them.
Why does a 502 come and go?
Because its causes do. A restart is over in moments. A busy spell fills PHP's workers and then eases. A plugin that crashes PHP does it only on the pages that run its code. Note which pages fail and at what times: that pattern is what the host needs.
Will raising PHP's memory or time limit fix a 502?
Not as a rule. A page that passes PHP's own memory_limit or max_execution_time is stopped by PHP, which reports it, and the visitor gets a 500 with "There has been a critical error on this website." Those have fixes of their own: Allowed memory size exhausted and Maximum execution time exceeded. A 502 means the server in front got no usable answer at all. That is what separates it from a 500 Internal Server Error, where the server behind did answer, with an error.
- GuideHow to change WordPress permalinks without breaking your old links
- GuideHow to deactivate WordPress plugins when you are locked out of the dashboard
- GuideLocked out of WordPress admin: find which lockout you have and get back in
- GuideSlow WordPress admin: how to find the cause and fix it
- ResourceWooCommerce checkout is down: a runbook
- GuideWordPress site not showing up on Google: what to check, in order

