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:
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:
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:
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:
If the service doesn’t restart on its own, you may have to inspect the server health and optimize it.
Some of the reasons for this sudden load are:
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:
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:
Then make the adjustments accordingly. Make sure that firewall is not blocking standard or non-standard ports.
If you get the following message in your error log file, it should confirm that there’s a bug in the app:
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!
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/"
- The backend service crashed
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
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 ]
- High load on the server
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
- Improper configuration
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
- Server port is blocked in Firewall
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
- Bugs in the App
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)
So those are some of the reasons why you get the 502 Bad Gateway error. For further assistance, contact the hosting support team!