How to Fix the 502 Bad Gateway Error in WordPress

How to Fix the 502 Bad Gateway Error in WordPress

Have you ever seen the very annoying and frustrating 502 bad gateway error on a WordPress site? Yes, I think we all have encountered the issue. Do you know what 502 bad gateway error means? Do you know how to solve the issue? In this blog post, we will provide you with detailed information about the 502 Bad Gateway Error in WordPress, and tell you how to troubleshoot the 502 Bad Gateway Error in WordPress as well. 

What does 502 Bad Gateway Error means?

The 502 Bad Gateway error is an HTTP status code that means that one server on the internet received an invalid response from another server. In more technical words, A 502 Bad Gateway means that the proxy (gateway) server wasn’t able to get a valid or any response from the upstream server.

The official documentation from W3.org, states that HTTP status codes starting with 5 (5xxx) relate to errors that occur when there’s a miscommunication between several servers.

If you are seeing a 502 bad gateway error on a website, it means that the origin server sent out an invalid response to another server that acted as a gateway or proxy. It can be a tricky investigation to locate what and where is the process which caused the issue. However, there are some general troubleshooting steps that you can follow to get it solved.

502 Bad Gateway Error Variations

A 502 Bad Gateway Error has several different variations due to the various internet browsers and various web servers and operating systems. Bellow are some of the most popular variations of the 502 Bad Gateway Error.

  • “502 Bad Gateway”
  • “Error 502”
  • “HTTP Error 502 – Bad Gateway”
  • “502 Proxy Error”
  • “HTTP 502”
  • “502 Service Temporarily Overloaded
  • “502 Server Error: The server encountered a temporary error and could not complete your request”
  • “502. That’s an error. The server encountered a temporary error and could not complete your request. Please try again in 30 seconds. That’s all we know.”
  • A blank white screen

How to Fix the 502 Bad Gateway Error (3 Possible Solutions)

A 502 bad gateway error is usually an issue with the network or server. But, it can also be a client side issue. So we will troubleshoot the error within the server, and the client side as well. The following are some steps to check and solve the issues.

1. Check your server

502 bad gateway

If you are seeing the error as shown in the picture, most probably the error is within your webserver.
Let’s check your webserver configuration, in this case the server is running Nginx. WordPress is running on PHP and MySQL. So, in nginx we need to check how PHP files are processed. Find the “location ~ \.php$” in your nginx server block.

ssh@your_ip -p7022

502 bad gateway error502 bad gateway error

According to the nginx configuration as shown in the picture, the PHP files are processed by fastcgi that is running on port 9000.
Let’s check what is running on the port

netstat -pltn | grep 9000

If you don’t see any result after issuing the command, then the process is died. This is why you see the 502 bad gateway error.

In latest CentOS and Ubuntu, you can invoke this command to see where the process should be ran from:

systemctl list-unit-files | grep fpm

502 bad gateway error

Issue this command to run the service:

systemctl start php7.0-fpm

Now, go to your web browser and check your website.

If you use CloudFlare and see the 502 bad gateway error as shown in the picture below, most likely your web server is down. You can try restarting apache or nginx, depends on which web server you use.

502 bad gateway error in cloudflare

2. Clear your cache.

If the first solution has been done, but you still see the 502 bad gateway error, then your web browser is still relying on its cache. We have to tell the web browser to load the latest version from the site. We can do this by pressing Ctrl + F5 on the keyboard (Windows/Linux), or Shift + Reload button in the toolbar (Safari browser), or CMD + Shift + R (Chrome and Firefox for Mac users).

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

3. Disable CDN

If you are using CloudFlare, you can try disabling CDN for a while.

502 bad gateway error - disable CDN

4. Check your scripts

As previously said, a 502 bad gateway error is due to an issue with the web server. Sometime, the php processes are killed by the server due to high memory usage, for example. This killing process will terminate requests after a certain of time, this will also cause the website down. But, usually the website will be automatically accessible because php processes is respawned at that time. To solve this issue permanently, you need to check which php script is consuming high memory.

5. Check Plugins and Themes

To troubleshoot which plugin is causing the error is very simple. Even we don’t have access to our WordPress backend, we can disable all plugins at once by renaming the plugins directory. To do it, please connect to your server using SSH. Note that you can also do this using an FTP client, like FileZilla.

fixing 502 bad gateway error
You can enable plugins one by one, please follow these steps:
Go to your wp-content directory, we can see that there is no plugins directory, we renamed it already.
Let’s create a plugins directory and move a plugin over. This will automatically enable the plugin.

fixing 502 bad gateway error in wordpress

Sometimes, troubleshooting WordPress errors is easier than we might think. But, also it could be very hard to solve at some point.


Of course, you don’t need to get troubled with your server if you have a WordPress VPS hosted with us – in which case, our technical support team will help you investigating and solving this 502 Bad Gateway issue immediately. They are available 24/7, and can cater to any questions or requests.

PS. If you liked this post, feel free to share it with your friends by using the social media share shortcuts below, or simply leave a comment. Thanks.

 

Leave a Comment