Bobcares

How to fix “502 bad gateway” error in Digitalocean Nginx server

by | Dec 11, 2018

Our site showed a “502 Bad Gateway” error. Restarting nginx brought the site back temporarily, but still getting the error. Can you take a look and fix this error?

That was an emergency request from DigitalOcean Droplet owner in our Infrastructure Management Services helpdesk.

Unfortunately, bad gateway errors make your website down and affects your business badly.

Today, we’ll see the top reasons for “502 bad gateway” error in Nginx and how our Support Engineers fix them.

What causes “502 bad gateway” error in Nginx?

Firstly, we’ll see the exact cause of the “502 bad gateway” error in Nginx.

These days, a common method to speed up websites is to put Nginx in-front of an Apache web server.

Here, Nginx handles the incoming requests and pass them to a back-end Apache server.

And, these 502 bad gateway errors happen when a front-end web server do not get a proper response from the back-end web server.

From our experience in managing DigitalOcean Droplets, the bad response from underlying services can happen due to various reasons.

Let’s now discuss further on it.

1. Bad Database server

It’s a common practice to use multiple Droplets for the optimized working of any application. This set up uses one Droplet for databases, another Droplet for web services and so on. Additionally, this gives us the flexibility to add more resources depending on the functionality of Droplets.

We often see that wrongly coded web scripts often overloads database servers, and this eventually cause websites to fail. And they show “502 bad gateway” error.

Similarly, when the mysql instance is not optimized for performance, it can also cause high load. This high load causes database server to crash. This again affects the working of websites.

 

2. PHP-FPM issues

Often PHP-FPM issues also cause bad gateway errors. Let’s check how this happens.

For optimum performance of websites, many Droplets use PHP Fast Process Manager (PHP FPM).

When using PHP-FPM, there is a master process that manages the individual php worker processes. It dynamically creates and terminates worker processes as per the requirement to handle php scripts.

PHP-FPM supports caching mechanism aka storing a copy of scripts in the memory. As a result, it saves time and resources for running PHP scripts.

Unfortunately, bad caching configuration, exceeding worker process limits, etc. can cause PHP-FPM to fail. Thus, it will not be able to provide response to the Nginx web server. As a result, visitors will see “502 bad gateway” error.

 

How to fix the “502 bad gateway” error in DigitalOcean Nginx?

We now know how the underlying services cause the “502 bad gateway” error in Nginx.

Now, let’s see how our Dedicated Support Engineers diagnose the faulty underlying service and fix them.

 

1. Checking server status

As the first step, we check the status of the server including server load, service status, etc. This helps us to get an idea on what exactly is causing the failure.

For instance, in a DigitalOcean Droplet, when the websites showed the “502 bad gateway” error, the process list was as shown in the picture.

502_bad_gateway

Here, MySQL service was using 769.1% of CPU and overloading the server. This helped us to identify the service causing the error.

To fix, we analyzed the top resource consuming MySQL queries and suggested customer to optimize them. A restart of the service made the websites functional again.

 

2. Checking logs

From our experience, we see that logs really helps to fix the bad gateway problem effectively.

When there is a problem, our Support Engineers watch the Nginx error log available at /var/log/nginx/error.log.

This log normally contains the reason for underlying problem. For example, in a Droplet where PHP-FPM was badly configured, the logs showed this:

[19-Sep-2018 10:15:21] NOTICE: [pool www] child 19485 started
[19-Sep-2018 10:15:22] WARNING: [pool www] child 18378 exited on signal 11 (SIGSEGV - core dumped) after 41.704231 seconds from start
[19-Sep-2018 10:15:22] NOTICE: [pool www] child 19487 started

Here, the php worker process was crashing and causing website failure. Therefore, our Support Specialists tweaked the PHP FPM configuration and installed Memcache as the caching mechanism. And, the websites started working successfully.

[Are your websites overloading the server? We can optimize your DigitalOcean droplet to serve websites faster.]

Conclusion

The Nginx error “502 bad gateway” happens in DigitalOcean Droplets when there is a bad response from the database server, PHP handler, etc. Today, we’ve seen the top reasons for the service failures and how our Expert Support Engineers fix them.

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";

2 Comments

  1. Valehero

    Why do I need Apache? Can not use Nginx as standalone server?
    I have already setup website using Nginx on Droplet but I have NOT installed Apache.
    If a script takes 20 seconds to execute then I get ERROR 502 after 15 seconds which is Nginx default connection timeout and empty page is retuned.

    Where can I adjust Nginx connection timeout?

    Reply
    • Visakh S

      Sure, you can set it up as a standalone server. Many people do that now with php-fpm as the PHP engine.

      The directive “proxy_read_timeout” in Nginx config /etc/nginx/nginx.conf controls the timeout from a proxied server. http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout

      If you have a PHP-FPM server in the backend, you may need to adjust the “fastcgi_read_timeout” value as well.

      Click on the chat button on the right to talk to a server admin if you are still facing this issue.

      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