How to Fix 502 Bad Gateway Error

How to Fix 502 Bad Gateway Error

502 bad gateway is one of the HTTP status codes that you can run into. It means that the edge server did not get a valid response from the inbound server. While not as frequent as the 500 internal server error or 503 service unavailable error, it can still give you quite a headache.

Each time you open a site, the browser sends out requests to other web servers. The servers then handle these requests and send back an HTTP status code along with the requested resources. Generally, the HTTP status codes are only displayed when a problem has occurred to inform users that something went wrong.

This tutorial focuses on the causes and fixes of the 502 bad gateway error on WordPress and regular websites.

How to Fix 502 Bad Gateway Error

While the 502 bad gateway error usually relates to server-side problems, it can also be caused by misconfigurations or issues from the client-side. Hence, we’ll overview the common troubleshooting steps with both causes in mind. While some solutions are focused on WordPress, most of them can be applied to any website. So, let’s see the steps to fix the 502 bad gateway error:

1. Refresh the Page

The first solution is a fairly simple one. Wait around for a minute or two and refresh the page you’re on. In a lot of cases, the error is temporary and a simple page refresh will do the trick. For a shortcut, Window users press F5 or CTRL + F5 and Mac users press command + R. Meanwhile, you may also try checking to see if the website is down using an online tool.

2. Clear Browser Cache

If the error persists, there is a chance that a certain cached element is causing it. Thus clearing the browser cache is a good approach when troubleshooting the 502 bad gateway error.

3. Try a Different Browser

Checking the connection using a different browser or incognito mode is also a great idea. It will let you rule out the option that the 502 error appeared due to a browser-related problem.

4. Flush DNS

The 502 bad gateway error can also pop up due to DNS issues, such as incorrectly cached IP address values. In such cases, flushing DNS is another thing that you can try. Here’s a tutorial on how to flush DNS on Windows, Mac, and Linux.

You may also try temporarily switching your default DNS server to Google Public DNS.

If you have recently migrated your WordPress site to a new host, remember that it can take up to 24 hours to properly propagate.

5. Test on Another Device

If none of the above solutions fixed the 502 bad gateway error, test the connection on another computer or a mobile device, preferably connected to a different network. This is the final step that will rule out whether the error is related to your machine or if the problem lies elsewhere.

6. Check Error Log

If the 502 bad gateway error appeared after a certain change or update, it’s possible that the fault lies within the website itself. The best approach is to check your error log.

In WordPress, you can enable error logging by adding the following lines in wp-config.php:

define( 'WP_DEBUG', true ); 
define( 'WP_DEBUG_LOG', true ); 
define( 'WP_DEBUG_DISPLAY', false );

All the generated entries will appear in wp-contents/debug.log file.

7. Check Plugins and Themes

It’s also important to check your plugins, themes, and extensions. Issues with caching plugins can often lead to a 502 bad gateway error popping up.

If you are still able to access your WordPress dashboard, go to Plugins, and simply click Deactivate from the Bulk Actions menu.

deactivate plugins on wordpress dashboard

Then, activate each plugin one by one, while refreshing your WordPress site after each one. Once the 502 error pops up again, you have found the problem plugin.

Another simple way to check if plugins are at fault is by disabling all of them for a short moment. To do it, navigate to the wp-content directory and rename the plugins directory.

If you use Hostinger, access your hpanel and click on Files. From there, go to File Manager -> public_html -> wp-content. 

rename plugins with disable If the website starts working after all plugins are disabled, you may rename the folder back to plugins. Then try disabling the plugins one by one to determine which one caused the error in the first place.

If the website still doesn’t work, follow the same steps with your WordPress themes.

8. Check CDNs

Another cause of the 502 bad gateway error can be CDN (content delivery network) or DDoS (distributed denial-of-service) mitigation services. One notable example would be CloudFlare, where the 502 bad gateway can occur in two different variations depending on the issue.502 bad gateway error when using CloudFlare - version 1 This screen indicates that the issue in on CloudFlare’s side, and to fix it you’ll need to reach out to their customer support team. Easily check CloudFlare System Status before doing so. Alternatively, you may choose to disable CloudFlare, though keep in mind that DNS propagation may take a couple of hours.

502 bad gateway error when using CloudFlare - version 2

However, if you see this version of the error, it means that the issue lies with your current hosting provider.

9. Check the Site

If the 502 error is still showing up it is possible that other people are having the same problem. There are many tools on the web to examine if a site is down, but a good one to use is Down for Everyone or Just Me or IsItDown. Simply input your URL and check the status of your website.

10. PHP Timeout

If you are importing WordPress files and a lot of them, you could see an error 502 bad gateway as a result of that. This will happen when a single PHP process runs longer than it should. The max_execution_time (max_input_time) is in your PHP configurations on your web server.

Talk to your web host if you want to know what your values are set at and if you can increase them.

11. Contact Customer Support

If none of the above solutions fixed the 502 bad gateway error, then it’s best to contact the customer support team. While describing the issue, try to include what troubleshooting steps that you took. Provide as much information as possible and you’ll surely be assisted.

502 Bad Gateway Error Variations

Since there are a number of browsers, operating systems, and web servers, the 502 bad gateway error may also appear with various names. Here are some alternatives that you can encounter:

  • Temporary Error (502)
  • 502 Error
  • Error 502
  • HTTP Error 502 Bad Gateway
  • 502 Server Error: The server encountered a temporary error and could not complete your request
  • 502 – Web server received an invalid response while acting as a gateway or proxy server
  • HTTP 502
  • 502 Proxy Error
  • Bad 502 Gateway
  • 502 Service Temporarily Overloaded
  • 502 Bad Gateway Nginx
  • 502. That’s an error
  • A blank white screen

Keep in mind that some websites have custom 502 error pages and will look different.

What Does 502 Bad Gateway Mean?

HTTP status codes that start with the digit “5” relate to errors that occur when there’s a miscommunication between several servers. Though the exact reason for their appearance varies, in most cases the cause lies within the server-side.

Getting a 502 bad gateway error means that the origin server sent out an invalid response to another server that acted as a gateway or proxy. Since all requests on the web are forwarded through multiple gateways, it can be quite tricky to locate the exact point or process which caused the issue. However, there are general troubleshooting steps that you can follow to fix it.

502 bad gateway error in browserConclusion

In this tutorial, we’ve learned what 502 bad gateway error is and how to approach it with basic troubleshooting steps.

In case you have any extra tips, tricks, or solutions, feel free to share them with us in the comments below.

The author

Domantas G.

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.