Nginx 502 Bad Gateway

502 Bad Gateway means that the server you’re attempting to access is showing an error due to a problem with another server. 

In this scenario, the server is actually a proxy that receives information from another server. 

If you’re getting this error in Nginx, we’ll help you troubleshoot the problem in this guide. 

Nginx 502 Bad Gateway Causes

Here are some of the possible reasons behind the Nginx 502 Bad Gateway error: 

  • Nginx isn’t running
  • There are firewall issues
  • The domain name is invalid
  • PHP-FPM isn’t running
  • PHP-FPM timeout 
  • The server is unavailable
  • There’s a high server load
  • You have services configuration issues

Nginx Is’t Running

You can find out whether Nginx is running or not by executing this command: systemctl status nginx.

If you get an “inactive” response, then you must start Nginx to fix the issue. 

Firewall Issues

If you have your firewall turned on, it might be blocking requests between the edge and origin servers. 

Even if the firewall isn’t causing an issue, certain CMS security plugins or DDoS protection systems may cause a similar problem. 

Invalid Domain Name

In this scenario, the domain name isn’t allowed because it’s not resolved to the IP address. Changing the DNS server and waiting around 24 hours for the new settings to propagate may fix this issue.          

PHP-FPM Not Running

If PHP-FPM isn’t running, you’ll get the 502 Bad Gateway error.

PHP-FPM (FastCGI Process Manager) is a tool that’s utilized to make a website faster. Compared to CGI techniques, PHP-FPM is capable of making a website much speedier with very fast load speeds. 

If your system runs on Linux, use the following command to check the status of PHP-FPM: ps aux | grep php-fpm. 

If you can’t see any PHP-FPM processes being executed, then you must run PHP-FPM to tackle the issue. 

PHP-FPM Timeout Expires

If PHP-FPM is running, but the server is taking too long to respond, you’re likely dealing with a PHP-FPM timeout. 

This issue can stop the connection before the response is sent, resulting in a 502 Bad Gateway error.

One way to tackle this is to search for your app to learn why it’s taking so long to respond. You can also try increasing the PHP-FPM timeout a bit.                           

Unavailable Server 

The server is unavailable if it isn’t online for some reason or there’s no connection to the server, even if it’s not down. 

High Server Load

If the backend servers are loaded beyond their capacity, you might get a 502 Bad Gateway error. 

There are multiple causes of load spikes, which include comment spamming, malware infections, app bugs, memory leaks, brute force attacks, and more.

The best way to deal with a high load issue is to pinpoint the exact resource that’s being exploited and find the source of abuse. 

Improper Service Configuration

Nginx servers integrate multiple systems and subsystems like Apache processes, DNS, and PHP services. These services must be configured properly in order for your Nginx server and backend servers to work properly. 

A misconfiguration in one or more of these services may cause the 502 Bad Gateway error. 

The most common configuration issues include:

  • DNS resolver problems that make domain lookups impossible
  • Capacity restrictions such as the number of connections for each IP
  • Wrong file limits for PHP apps
  • Incorrect DB login credentials, particularly in migration or upgrade processes

However, configuration errors are often hard to diagnose. The only way to do it is to scan the error log and understand what’s going on, but you need to have a good knowledge of web server settings and PHP.  

If there’s a server administrator, you can simply ask them. 

Tracking System Performance to Prevent Problems in the Future

Many open source platforms integrate monitoring and analysis tools like Graphite and Grafana. 

Hosted Graphite can help you track your system performance to prevent the 502 Bad Gateway error. It’ll also help you spot errors and bottlenecks. It’s basically a tool for collecting and processing metrics. 

Hosted Graphite can also act as a data source for Grafana since it has a built-in Graphite query editor for adding functions and writing queries. 

With Grafana, you can view web-based data visualization reports and analytics. It also makes it easier for you to create customizable dashboards. You can even use annotations to add comments and create logs. 

On top of that, Grafana enables you to incorporate third-party plugins to add more features and functionality. It also supports SQL. 

Conclusion

So these were all the possible causes of the Nginx 502 Bad Gateway error. 

Finding the root cause of the problem is the trickiest part of solving it, but if you follow this guide, you can save yourself hours of researching and deal with the issue much faster. 

Leave a Reply