Bobcares

How to fix ‘502 server error – Bad Gateway’ in web servers

by | Aug 1, 2017

Google forums get bombarded by customer complaints whenever they see a ‘502 server error – Bad Gateway’ in their Google applications. Though an intermittent error, it can often shoo away visitors from your website.

Bobcares helps website owners and web hosting providers fix all website and application errors, and this error is often reported at our Website Administration Services.

See how we can support your websites!

The 502 Bad Gateway error in sites can present itself in different ways, such as ‘502 – Service Temporarily Overloaded’ or ‘HTTP Error 502 – Bad Gateway’.

Recently, we were contacted by a VPS server owner whose customers were complaining about 502 temporary errors on all sites every hour. This was affecting his business badly and wanted a permanent solution.

What causes ‘502 server error’ in web servers

A 502 error means that a web server that is serving as a reverse proxy (say, Apache) for the website origin server (such as NginX), did not receive a valid response from the original web server.

Upon examining the server, we could see that the Apache web server in it was running as an NginX proxy. The web server was restarting the http service about once an hour.

In our experience handling 502 server errors, we have seen that a ‘502 Bad Gateway’ error happens due to one of these reasons:

  1. An overloaded server – A web server can crash often due to resource crunch such as memory shortage, which can be caused by abusive users or process overloads.
  2. Web server frequently restarting – Due to any configuration errors, module failures, too many applications and processes running on it or even any attacks, the web server can restart frequently and cause temporary 502 servers errors in websites.
  3. Bad code in websites – Websites that run legacy applications or sites with improper programming code, can tamper with its proper functioning, and show up errors intermittently.
  4. Network related errors – Other network related errors such as DNS issues, routing problems, firewall blocks in server, ISP related issues, also can lead to 502 server errors such as ‘502 Bad Gateway’.
  5. Server software timeout – This error can also occur when requests at the NginX server takes more time to complete and a caching tool such as Varnish Cache reaches its timeout value in that time. Other issues such as slow queries can also cause 502 errors.

[ Worried about your broken websites? Get our server specialists’ assistance to fix your site from all errors. ]

How to fix 502 service error in NginX web server

It is important to identify the actual cause of the 502 server error before pinpointing on the actual solution. We examined the web server logs during the restart times and identified the ‘seg fault’ errors during that time.

During further investigation, we examined the configuration settings of the web server and found that there was a missing module ‘mod_rpaf’, which was causing the web server to crash.

root@server [~]# ls -l /usr/local/apache/modules/mod_rpaf-2.0.so
/bin/ls: cannot access /usr/local/apache/modules/mod_rpaf-2.0.so: No such file or directory

Rpaf is the reverse proxy add forward module designed for backend Apache servers. It’s useful when you are running Nginx as front end server, and you need to get the real IP of the backend requests.

Since the module was conflicting with the Apache-2.4 version, we patched it further to get it working fine. Once the recompilation was done and Apache was restarted, the segmentation fault issues stopped.

We monitored the web server for some more hours and noticed that it was working fine without restarts or giving any 502 server errors in websites.

Some other fixes that help in avoid 502 server errors for websites are:

  • Keeping the website files such as plugins and themes updated and relevant.
  • Optimising and fixing slow MySQL queries in the server.
  • Auditing the web server software and maintaining the modules up-to-date.
  • Avoiding any routing issues and monitoring the web server for any overload or attacks.

[ A single misconfiguration may break your site! Click here for a customized and error-free website to help you boost your business. ]

Bobcares helps online businesses of all sizes achieve world-class security and uptime, using tried and tested solutions. If you’d like to know how to make your server more reliable, we’d be happy to talk to you.

 

Is your website stable and unique for your business?

Don't let a poor website design or site errors affect your business. We're here to help you.

CLICK HERE TO GET AN ERROR-FREE SITE

var google_conversion_label = "Blp0CLCojHIQ0aD71QM";

4 Comments

  1. BG

    Excellent article, taking the matter down to specific code level. From presenting symptoms to testing resolution of the issue, the article demonstrates every aspect clearly.

    It so happens that I arrived here, early this morning, in search of background on a “502 Bad Gateway” error, but had no further context. What a pleasure to find somebody had put extra effort into writing an effective technical support article.

    To your credit, you backpedaled from the possibility my 502 error was real-time evidence of a DDOS attack. More likely, the 502 error was simply a human oversight (*strangely, we term congressional committees charged with monitoring sensitive and important matters an “oversight committee”. Perhaps that applies more often than it should.)

    Reply
    • Reeshma Mathews

      Thanks for taking time to share such a detailed feedback 🙂

      Reply
  2. Nik

    Was trying to purchase a psn card top up and received this message .. I’m not sure if it’s due to international credit card entered which is not accepted or they have a firewall since my ex is hacking) into my network … are you able to help me with anti hacking issues my ex has foreign govt access . It’s been 8 years and I want my life back

    Reply
    • Maheen Aboobakkar

      We’ll be happy to talk to you on chat regarding the issue(click on the icon at right-bottom).

      Reply

Submit a Comment

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

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

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
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

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

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

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