Bobcares

How to fix PHP 502 bad gateway error in websites

by | Nov 14, 2018

"My WordPress site is getting lot of bad gateways when trying to edit the back end. Please fix."

 

Recently, we were contacted by a customer who was getting PHP 502 bad gateway error when trying to update his website.

In our role as Technical Support Services for web hosting companies, configuring and managing the web servers for best performance, is a major task we do.

Resolving web server errors forms a part of this service. Today, we’ll see how we fix 502 gateway errors in our customers’ servers.

What is PHP 502 bad gateway error?

A gateway in a server denotes a connection point between two services in it. Gateway errors happen when front-end web server does not get a response from back-end web server.

502 gateway error usually happens in these scenarios: Nginx or Apache is front-end web server & Apache, PHP-FPM or other services is in back-end.

When the front-end server is unable to establish a proper connection to the back-end server, the websites served by these web server starts showing PHP 502 bad gateway errors.

The easiest way to resolve this error is to restart the services. But that is only a band-aid fix and you will soon find the same error recurring in your sites.

In the servers that we manage, we examine the service related logs and website logs to figure out what exactly is causing this error, and then proceed to fix it.

 

PHP 502 bad gateway error – Major causes

In our experience dealing with gateway errors, we’ve come across a number of causes that turn out to be the culprits behind the error.

1. Service errors

The first step we do is to verify the status of the back-end service. If the service is not running or not giving the desired results, gateway errors pop up.

That back-end service can be Apache, PHP-FPM or other services such as Perl, Python, etc. 502 errors also occur when the service is overloaded or keeps crashing.

2. Configuration issues

Most often, even if the back-end service is in running status, it may not be configured adequately to serve the front-end web server purpose.

Certain websites are very demanding and with huge traffic. But the website specific service configuration may not be adequate to handle this demand.

If the configuration is not proper, services such as PHP-FPM or Nginx can time out handling the web server requests. This leads to PHP 502 bad gateway errors.

3. Mod-security restrictions

Mod-security is a web application firewall (WAF) configured to protect websites from attacks. This WAF is configured based on rules that filter incoming traffic.

Some of these rules can create unwanted complications and disrupt the service functioning. We have noticed 502 gateway errors in websites due to some messy Mod-security rules.

4. Cloudflare settings

Cloudflare is a CDN service that offers DDOS protection to websites. But for sites with too many redirects, this causes 502 errors at times, if not configured correctly.

5. Port or permission issues

In a gateway setup, there would be two services connecting to each other. These services would be running in separate ports.

For instance, in a server where reverse proxy is configured, Nginx (front-end server) would be listening on port 80 and Apache (back-end server) would be on port 8080.

These ports can be further changed for security reasons. If the ports are not allowed for connections in the server firewall, the connectivity can fail and lead to gateway errors.

Services such as PHP-FPM can also be configured to bind to socket files in the server. If the ownership, permissions or path to the socket file in the configuration file is improper, errors can occur.

6. Software bugs

In cases where custom software or some third party applications are configured as back-end services, any bugs in these software can lead to 502 errors.

7. High server load

Server load can spike when the website traffic is high or when a DOS attack occurs to the server. Peak traffic or malware files in the server can contribute to this high server load.

Certain 3rd party applications or not optimised website code can hog the server resources such as CPU and memory, and cause high load. This can lead to timeout issues and website errors.

 

Solutions for PHP 502 bad gateway error

Finding the root cause of the 502 error and resolving it, is the key to prevent intermittent website down times. The solution for the error varies based on the issue noted in the logs.

  1. In cases where the underlying service itself is not running, our primary focus is to figure out the problem and fix the service. This is done with the help of service or application or website logs.
  2. If the service is working fine, but logs show timeout errors, then we increase the execution time, buffer size and timeout settings of the service or application. But this value should not be increased blindly, as it can hog the server resources.
  3. The configuration file for the service is examined and settings such as socket path, port, user and group etc. are confirmed to be correct. Reviewing the website specific settings is also vital to pinpoint the issue.
  4. The port connectivity, firewall rule or related socket file is checked and proper access rights and permissions are granted for its functioning.
  5. Problem aspects such as APC cache, Cloudflare settings, redirect rules, mod-security configuration, etc. are reviewed, and issues, if any, are sorted out.
  6. To prevent a single process from hogging the entire server memory, we set memory limits of processes and services and tweak them to use the maximum allotted memory for that service.
  7. At times, a problematic web code or a query can tie up the resources, take a lot of time to complete execution, and cause errors. We identify such problem-makers and take actions to fix them.
  8. We review the number of processes configured for services such as PHP-FPM and Nginx or Apache. By tweaking these process count to the ideal values supported by server resources, we ensure that service errors are fixed.

 

 

Conclusion

Today we’ve discussed how our Support Engineers fix PHP 502 bad gateway in the servers we manage, and ensure error-free websites for our customers.

In our customers’ servers, monitoring the server resources periodically and conducting periodic server audits and tweaks enable us to prevent such bad gateway errors to a great extent.

 

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.

SEE SERVER ADMIN PLANS

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

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.