How can we help you today?

How to Resolve 502 Gateway Errors

Created by: Jesper Verkade

Modified on: Wed, 10 Jun, 2020 at 11:23 AM

A 502 Bad Gateway error indicates there has been an internal error within PHP, which resulted in the site not being rendered and therefore you will see this error in your browser.


How to Check Whether 502 Errors Have Occured

You can see whether any 502 errors have occured by analyzing the Nginx access logs. An easy way to do this is by using the Parse Nginx Logs (pnl) tool. Log in on your Hypernode and run the following command to check whether any such errors occurred today (change --today to --yesterday in order to check the errors for the day before).

pnl --today --filter status=502

The output will show you which requests could not be handled by PHP.

What Can Cause 502 Errors

Memory Depletion

The main cause is that PHP has ran out of memory. This does not normally happen, but if it happens, it is likely due to two reasons:

  1. The code base is very large (many installed modules or very large/complex modules)
  2. One or more Magento modules leak memory.

Hypernodes can generally cope with high memory usage, but in extreme cases the Hypernode will perform an automatic recovery restart of the PHP service. This might take a few seconds, during which visitors might get a 502 response.

To verify this situation, log in to your Hypernode and check the memory usage (in KB) of PHP processes:

pgrep -f fpm | xargs ps -o rss=

This will generate output like the one below:


This shows memory usage of 28, 39 and 251 MB. The average memory usage per process on our platform (across all customers and sizes) is 63 MB. If you are well above this figure, it might indicate an issue with your code. The solutions are:

  1. Fix the code, or replace a faulty Magento module. This requires a memory profiler, or the laborious process of trial and error.
  2. Upgrade to a Hypernode with more memory.

Low Level PHP Errors (SEGFAULT)

In very rare cases, PHP might crash for other reasons than memory depletion (a so called “SEGFAULT”). If this happens, the server process will automatically be restarted in a few seconds. If you encounter this often (verify with /var/log/php-fpm/php-fpm.log), it will likely help to switch PHP versions (e.g. from PHP 5.6 to 7) or disable IonCube.

Jesper is the author of this solution article.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.