wesupport

Need help?

Our experts have had an average response time of 13.14 minutes in February 2024 to fix urgent issues.

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

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 *

Categories

Tags