When accessing a page or web, you certainly sometimes experience a number of disturbances or errors when entering the destination site. One of the errors that are often encountered is appearing with the error code 502 bad gateway. Generally, this error code will appear in various forms such as:

  • 502 Bad Gateway
  • 502 Service Temporarily Overloaded
  • Error 502
  • Temporary Error (502)
  • 502 Proxy Error
  • 502 Server Error: The server encountered a temporary error and could not complete your
  • HTTP request 502
  • 502. That’s an error
  • Bad Gateway: The proxy server received an invalid response from an upstream server
  • HTTP Error 502 – Bad Gateway

So what causes the 502 bad gateway error? Some of the causes of the 502 bad gateway error are generally due to the browser cache that has not been cleared, plugin or theme problems that need to be updated, to DNS problems.

502 Errors Impact on SEO

Unlike 503 errors, which are used for WordPress maintenance mode and tell Google to check back at a later time, a 502 error can have a negative impact on SEO if not fixed right away. If your site is only down for say 10 minutes and it’s being crawled consistently a lot of times the crawler will simply get the page delivered from the cache. Or Google might not even have a chance to re-crawl it before it’s back up. In this scenario, you’re completely fine.

However, if the site is down for an extended period of time, say 6+ hours, then Google might see the 502 error as a site-level issue that needs to be addressed. This could impact your rankings. If you’re worried about repeat 502 errors you should figure out why they are happening to begin with. Some of the solutions below can help.

502 Bad Gateway Fix

Here are some tips you can try from your web browser to hopefully clear the “502 Bad Gateway” error and get your webpage displayed.

1. Try Reloading the Page

The error could be caused by a glitch with the server as opposed to an outage, and server connectivity issues can be fixed reasonably quickly in most cases.

Start by reloading the page – click on the circular arrow near the URL address bar. In Chrome and Safari, it’s on the left, in Firefox it’s on the right. Wait a minute or so before reloading the page. If the web page displays, then the problem has disappeared.

Another quick thing you could try is to open a new browser session. Close all your open browser windows, then open a new window and try navigating to the web page again.

2. Try Clearing Your Browser Cache

Your web browser saves information from each website you visit in the browser cache. Whenever you re-visit a website, the pages load quicker because your browser uses what’s stored in the cache.

However, this setup isn’t always a good thing. If your browser’s cache has an outdated or corrupted version of the website, this can cause the 502 error. Consider clearing your browser’s cache to get the most up-to-date version of the web pages your trying to reach. Here’s how that’s done:

Chrome

  1. In your Chrome browser window, click on the three-dotted menu in the top right.
  2. Select the “More Tools” option.
  3. Select the “Clear Browsing Data…”
  4. In the “Clear browsing data” pop-up, ensure only the “Cached images and files” option is checked, decide on the time range, then “Clear data.”

Firefox

  1. In your Firefox browser window, click the hamburger icon in the top right.
  2. Click “History.”
  3. Select “Clear Recent History…”
  4. In the popup, at the “Time range to clear” pull-down, choose “Everything.”
  5. Ensure only the “Cache” box is checked, then “Clear Now.”

Safari

To empty the cache in your Safari browser, the “Develop” menu needs enabling. To do this:

  1. Go to “Preferences” then “Advanced.”
  2. Check the “Show Develop Menu in Menu Bar” box.
  3. Go to “Develop” then “Empty Caches.”

Check With Your Host

The primary reason we typically see a 502 bad gateway error is due to an issue with the web host. Sometimes a query, script, or request will take too long and so it is canceled or killed by the server. A lot of hosts, especially shared hosts implement what they call a “kill script.” Basically what this does is it will terminate a request after a certain period of time so that it doesn’t take a site down or impact other users. If you do a Twitter search for “bad gateway” you can see thousands of tweets happening every day alerting their hosting company of the problem.

One reason you might want to consider moving to a host like Kinsta is that our platform runs in an isolated software container that contains all of the software resources required to run the site (Linux, NGINX, PHP, MySQL). This means that the software that runs each site is 100% private and is not shared, even between your own sites. This greatly reduces the chance of seeing a 502 bad gateway error. We aren’t running aggressive kill scripts like some shared hosts are. And even if one of your sites does happen to go down, it won’t impact your other sites.

Another reason you could see a 502 gateway error with your host is also due to the fact that the server is simply not reachable, either because it is down or there is no connectivity. In this case, it is recommended to check your host’s status page or open a support ticket.

502 Bad Gateway in NGINX

PHP-FastCGI Process Manager (PHP-FPM) is a background process for handling web server requests for PHP apps. NGINX passes web requests onto PHP-FPM worker processes that execute the PHP application. NGINX returns a “502 Bad Gateway” error when it can’t successfully delegate a request to PHP-FPM or if PHP-FPM doesn’t respond.

Here are the most common causes for NGINX returning a 502 error:

  • PHP-FPM is not running.
  • NGINX is unable to communicate with PHP-FPM.
  • PHP-FPM is timing out.

Now let’s see how to confirm whether any of the above is the problem, and how to fix it.

1. Check Whether PHP-FPM Is Running

When PHP-FPM isn’t running, NGINX will return a 502 error to all requests intended to reach the PHP application. You can use a “ps” command via a Linux host to check for running PHP-FPM processes. The command is:

sudo ps aux | grep ‘php’”.

If the results from this command don’t display any PHP-FPM pool or primary processes, PHP-FPM needs to be running to fix the 502 errors. To make your PHP app more reliable, consider using “systemd” to manage PHP-FPM as a service in your production environment. PHP-FPM’s background process will start automatically serving your PHP app whenever a new instance opens, or your server begins.

Since PHP-FPM is built-in to the PHP code, you can add it as a “systemd” service when you set up PHP. Once the project is set up as a service, use this command for an automatic start:

sudo systemctl enable php7.2–fpm.service”.

2. Check Whether NGINX Has Access to the Socket

Once PHP-FPM starts, it generates Unix or TCP sockets to connect to the NGINX web server. The PHP-FPM worker processes need the sockets to listen for NGINX requests. Ensure that PHP-FPM and NGINX are set to use the same socket

PHP-FPM uses a different config file per process pool. To find these files go to:

/etc/php/7.2/fpm/pool.d/”.

Pool sockets are outlined in a “listen” instruction in its config file, e.g. the following “listen” instruction, configures a pool called “mypool” to use a Unix socket found at: /run/php/mypool.sock”:

listen = /run/php/mypool.sock”.

If NGINX is unable to access a pool’s socket, you can work out which worker pool it is by examining which socket is specified in the NGINX error log. If PHP-FPM didn’t start the “mypool” worker pool, for example, NGINX would send a 502 error and the log entry would look similar to this:

Connect () to unix: :/run/php/mypool.sock failed (2: No such file or directory)”.

3. Check Whether PHP-FPM Is Timing Out

When your application takes too long to respond, users will receive a timeout error. If PHP-FPM’s timeout – fixed in the pool’s configuration (“request_terminate_timeout”) instruction – is less than NGINX’s timeout, NGINX will return a 502 error.

You can increase PHP-FPM’s timeout setting in the pool’s configuration file. However, this could cause another problem: NGINX could time out before getting a response from PHP-FPM.

NGINX’s default timeout is 60 seconds. If you’ve increased the PHP-FPM timeout over 60 seconds, and your PHP app hasn’t had time to respond, NGINX will issue a “504 Gateway Timeout” error. Avoid this by increasing your NGINX timeout setting.

502 Bad Gateway in Cloudflare

Cloudflare will return a 502 error when it’s unable to start a valid connection with your website’s source webserver or if the Cloudflare service is unavailable or configured incorrectly. The most common causes include:

  • The source server is overworked with too many server loads triggering server downtime
  • WordPress websites with low PHP memory limits or too many MYSQL connections can cause the server to load slowly
  • Incorrect DNS records, plugin or theme conflicts in WordPress, and service failures e.g. PHP-FPM and cache services

Try the following tips to resolve the”502 Bad Gateway” error from Cloudflare:

1. Reload the Page and Clear Your Cache

Reloading the webpage should be the first thing you try for any error message. The 502 error, specifically, may be the result of the server being overloaded causing temporary connection issues. Wait for a minute or so before reloading the page; this typically solves it.

If the 502 error still shows, clear your browser cache then try again. Long press Ctrl + F5 for Windows and Linux browsers. For Chrome and Safari on Mac, it’s Cmd + Shift + R.

2. Check Your Plugin/Theme for Conflicts

If you use WordPress, the 502 error may be down to a misconfigured plugin. To troubleshoot whether this is the cause, disable your plugins – no data will be lost.

  1. Sign in to your WordPress account.
  2. Select “Plugins” from the sidebar.
  3. Apply a “Deactivate” to all your plugins.

If the 502 error is solved, then you need to find which plugin is a problem. To do this, individually reactivate the plugins. After reactivating each one, reload the web page. When the Cloudflare error happens again, you’ll know which plugin is the culprit. You can request help from the plugin developer by posting a ticket in WordPress.

Ensure your themes and plugins are the latest versions, and your PHP version is supported.

3. Temporarily Disable Your Firewalls and CDN

The problem could be caused by the Content Delivery Network (CDN) or firewalls. This is a common issue with providers with extra firewall layers like Cloudflare. You can either check Cloudflare’s online status page for information on any problems with their service, or you can contact them.

If it’s confirmed that the problem is with your firewalls or CDN, try disabling them temporarily, then enable them after.

4. Check Your DNS Servers

Sometimes your Domain Name System (DNS) servers can return the 502 error because your website’s domain may not be pointing to the right IP address. If you’ve recently moved your website to another hosting service then you’ll need to wait for at least 24 hours for the propagation across the Internet to complete.

If this is not the case, to clear IP addresses and other DNS records from your local DNS cache you can try flushing it, here’s how:

  1. Launch a command window.
  2. Enter “ipconfig /flushdns”.

Or via a Mac:

  1. Open a terminal window.
  2. Enter “dscacheutil -flushcache”.

502 Bad Gateway Solved!

The “502 Bad Gateway” error message is a standard response when there is something wrong with communications between the servers involved in returning your web request.

error: Content is protected !!