Why the 502 Bad Gateway Error Occurs in Nginx? Five Potential Reasons and the Solution

Why the 502 Bad Gateway Error Occurs in Nginx? Five Potential Reasons and the Solution

502 Bad Gateway is a server error that occurs due to multiple reasons. It’s quite common among Nginx servers. At first occurrence, you might not be able to point out the exact reason. Thus, you won’t be able to solve the problem quick enough. If you’ve come across this error and want to fix it ASAP, this article is for you.

Learn why this error occurs and the ways you can fix it.

Five Reasons Behind the 502 Bad Gateway Error

The 502 Bad Gateway error is a cryptic error. So before you can fix it, you need to dig into the error log, which looks like this:

Code:
2017/04/04 08:34:43 [error] 949#949: *7 connect() failed (111: Connection refused) while connecting to upstream, client: XXX.XXX.XXX.XXX, server: myserver.com, request: "GET /myurl-this/ HTTP/1.0", subrequest: "/redis-fetch", upstream: "redis://127.0.0.1:6379", host: "refserver.com", referrer: "http://referalsite.com/myurl-this/"
Some words included in the message like ‘failed’ and ‘refused’ should give you a hint that something is not right with the servers. Here are five potential problems:
  • The backend service crashed
Nginx relies on certain backend services like PHP-FPM and cache and database services to function and orchestrate web applications.

When any one or multiple backend services stopped working for any reasons and crashed, you’ll get the 502 Bad Gateway error.

The common services which are most likely to fail are:
  • PHP
  • Apache
  • Cache
  • Database
The reason why these services may fail might be spike in traffic or resource limitations. It may also hit at an DDoS attack.

If you find that one of the backend services has crashed, you should kill the unresponsive process and restart it. Here’s the code to do that:

Code:
# kill -9 $(pgrep php-fpm)
# /etc/init.d/php-fpm restart
* Restarting PHP FastCGI Process Manager php-fpm [ OK ]
If the service doesn’t restart on its own, you may have to inspect the server health and optimize it.
  • High load on the server
Another common reason why you may have to deal with the 502 Bad Gateway error is because of high server load. When there’s a sudden jump in traffic and the server couldn’t cope with it, it throws the Gateway error.

Some of the reasons for this sudden load are:
  • There’s a spike in website traffic (too many people visiting the website at once)
  • Malware on the server
  • Comment spamming bots
  • Brute force attacks’
  • Resource hogging or memory leaks due to bugs
To fix this issue, you need to inspect the resource and look at I/O, Memory, CPU, and Net. Zero in on the service that’s abusing the resource and either terminate it or remote it altogether.
  • Improper configuration
If your server isn’t configured properly, there’s a chance you will get this error.

You may know that the Nginx server relies on backend service and other sub-systems to function. If these independent services aren’t configured as needed, they will fail to respond.

Here are some common configuration-related issues behind the 502 Bad Gateway error:
  • The DNS resolver is not configured properly, which causes domain lookup service to fail
  • DB login credentials are incomplete (which happen after migrations, upgrades, or restoration)
  • The Apache firewall settings (mod_security) are incorrect and causing the Apache to fail
  • Memory and file limit are conservative for the PHP applications to function
  • The capacity limits like allowed number of connections per IP are restrictive
It isn’t easy to pin point the configuration-related issues. The best way is to inspect the error log and try to find out the root cause.
  • Server port is blocked in Firewall
Firewalls are of utmost importance but they’re also root of certain issues. If you haven’t set them up correctly, the firewalls lead to many errors, the 502 Bad Gateway error being one of them.

Let’s take an example. In most Linux servers that are powered with Plesk automation suite, the port number for Nginx is 80. Likewise, Apache runs on 7080.

But many firewalls also block port 7080. This results in Apache not getting access to Nginx.

To fix this, you need to find out what port the services run on using this command:

Code:
# netstat -lpn
Then make the adjustments accordingly. Make sure that firewall is not blocking standard or non-standard ports.
  • Bugs in the App
At times, the web application that’s running on the server contain bugs. If debugging tools fail to detect it, the bug can cause the server to slow down and crash.

If you get the following message in your error log file, it should confirm that there’s a bug in the app:

Code:
[notice] child pid 27831 exit signal Segmentation fault (11)
In such cases, you need to sit down with the developer and figure a way out. Check the software documentation and requirements and make necessary adjustments. For the time being, you may take down the portion of the web app that contains the bug.

So those are some of the reasons why you get the 502 Bad Gateway error. For further assistance, contact the hosting support team!
Author
kumkumsharma
Views
2,035
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top