Need help?

Our experts have had an average response time of 11.7 minutes in August 2021 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

Magento 502 bad gateway error – Causes and Fixes

by | May 15, 2019

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.

[email protected]:/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;
fastcgi_buffer_size 32k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;

[Is your Magento site showing bad gateway error ? Contact our Magento experts right away.]

 

Conclusion

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.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

1 Comment

  1. Arshad

    thanks its very useful

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF