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.
TABLE OF CONTENTS
- How to Check Whether 502 Errors Have Occured
- What Can Cause 502 Errors
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
--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
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:
- The code base is very large (many installed modules or very large/complex modules)
- 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:
28720 39408 251184
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:
- Fix the code, or replace a faulty Magento module. This requires a memory profiler, or the laborious process of trial and error.
- 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.