Creating a shopping website using Magento is easier than you think. With features like easy customization, faster checkout, mobile friendliness Magento is one of the best eCommerce platform.
But, things go for a toss, when Magento website throw errors like “502 bad gateway“. Although this happen when PHP process crashes, it can be tricky to find the exact reason for the crash.
At Bobcares, we help customers to fix Magento 502 bad gateway errors as part of our Server Administration Services.
Today, we’ll see the top causes for Magento 502 bad gateway errors and how Bobcares Engineers fix them.
What is 502 bad gateway error?
Let’s first have a quick look on 502 bad gateway error as such.
Web servers like Nginx hand over process requests to the underlying services and get back the results and display it. So, if any of these services crash or freeze, Nginx won’t get any data from them, resulting in “502 Bad gateway” error.
For example, when you access a PHP website via browser, Nginx forwards request to PHP. Further processing is done by the PHP handler on the server. In simple words, when PHP cannot give the requested results, it shows error on the website.
How does Magento 502 bad gateway error look?
Till now, we checked about the generic 502 bad gateway error. It’s time to see how this happens in Magento websites.
Magento web sites make use of PHP to work. When the Nginx server get a website request, it pass on the requests to PHP handlers on the server. It is the PHP handler that provide the results back to the web server. Unfortunately, due to bad PHP configuration, stopped PHP service, or even broken PHP code, Nginx may not be able to get back the results. And, this ends up in Magento 502 bad gateway error as shown below.
Top causes and fixes for Magento gateway error
From our experience in managing Magento websites, we often see gateway errors in websites. Let’s now see the top reasons for the error and how our Dedicated Engineers fix them.
Exceeding PHP limits
As we already saw, a major share of Magento 502 bad gateway error happens due to PHP issues. This can happen when PHP has ran out of memory, PHP timeout values are low and so on. Luckily, the exact reason for error can be seen from the PHP error logs for the website.
By default, PHP works based on the configuration values set in the php.ini file. It can be set for a single website or for all websites on the server.
In some Magento websites, PHP goes out of memory when the code base is very large. This happens when there are many installed modules or website uses very complex modules. And, when one or more Magento modules leak memory, website shows 502 error.
How we fix PHP limits
Firstly, we login to the server as root user and check the memory usage (in KB) of PHP processes. A sample result from one server looks as below.
$ pgrep -f fpm | xargs ps -o rss= 18520 29870 357484
Here, the top memory consuming process was linked to a website with faulty Magento module. We disabled the module from Magento admin panel and the website started showing up correctly.
Similarly, we also check the memory usage on the server. When there are memory constraints, we suggest customer to upgrade memory on the server. The memory usage on the server will be back to normal. A sample memory output will be as shown.
root@ip-172-xx-xx-xx:/var/www/html/magento2# free -m total used free shared buff/cache available Mem: 16046 1179 12531 216 2335 14275 Swap: 0 0 0
PHP errors can happen due to other timeout values, directory restrictions and so on. In such cases, our Dedicated Engineers tweak the values in the php.ini file to make the website working again.
Nginx configuration limits
Again, Magento 502 bad gateway errors happen due to certain limits set in Nginx configuration too.
Recently, when a customer reported problem with his Magento website, the error logs said this:
*1 upstream sent too big header while reading response header from upstream, client: 127.0.0.1, server: abc.domain.com, request: "GET /my-page/ HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.1-fpm.sock:", host: "abc.domain.com"
In this server, the fastcgi buffer settings was causing the problem and PHP was not giving proper results to the Nginx server. As a result, website reported errors.
How we correct Nginx
In this case, our Dedicated Engineers first determined the correct buffer sizes as per the website requirement.
Then, we increased the values for fastcgi_buffers and fastcgi_buffer_size in the Nginx configuration file located at /etc/nginx/nginx.conf.
Again, when Nginx runs as reverse proxy, the fix involves increasing the proxy_buffer_size and proxy_buffers too.
In servers with control panels like Plesk, the tweak becomes easy. A sample set of directives that we add in the Additional nginx directives looks as:
fastcgi_buffers 16 16k;
proxy_buffers 4 256k;
[Is your Magento site showing bad gateway error ? Contact our Magento experts right away.]
Precisely, Magento 502 bad gateway error mainly happens due to problems with underlying PHP process. Today, we saw how our Support Engineers properly diagnose and fix these errors and make Magento websites available.