Understanding NGINX 502 Bad Gateway: PHP-FPM

Understanding NGINX 502 Bad Gateway: PHP-FPM

Table of Contents

  1. Introduction
  2. What does NGINX 502 Bad Gateway mean?
  3. What is PHP-FPM used for?
  4. Possible reasons for 502 Bad Gateway
    1. NGINX is not running
    2. PHP-FPM is not running
    3. PHP-FPM timeout expires
    4. Firewall blocks requests
    5. A domain name is not allowed
    6. Server is unavailable
  5. How to solve 502 error?
    1. As visitor
    2. As developer
  6. Monitoring your system metrics with Hosted Graphite by MetricFire
  7. Conclusion

Introduction

Error 502 Bad Gateway is common among website users. There are various possible reasons for this error and different ways to solve it. In this article, we will look at the main possible causes and how they can be solved by users and web developers.

                 

Use the Hosted Graphite platform to analyze your system’s performance and troubleshoot errors. For getting more detailed information about Hosted Graphite and how to integrate it with your system, book a demo with our team or sign up on Hosted Graphite for the free trial.

      

What does NGINX 502 Bad Gateway mean?

502 Bad Gateway means that the server you are accessing receives an error from another server. This happens when one server acts as a proxy to receive information from another server. When connecting to another server, it returns an error. NGINX returns error 502 if it cannot connect to PHP-FPM or PHP-FPM is not responding.

          

There are different names for the 502 error that you can see on different sites. For example:

  • HTTP Error 502 - Bad Gateway.
  • HTTP 502.
  • 502 Service Temporarily Overloaded.
  • Temporary Error (502).
  • 502 Bad Gateway NGINX.
  • 502 Bad Gateway.
  • Error 502.
  • 502 Proxy Error.

             

Also, the appearance of the page that displays the 502 error can be customized on different sites in the same way as it is done for the 404 not found error. There are different causes of error 502. Below we will look at the main ones, as well as possible ways to fix this error.

                            

What is PHP-FPM used for?

PHP-FPM (PHP-FastCGI Process Manager) is a web request processing tool for PHP applications. PHP-FPM together with NGINX can significantly increase the performance of websites and reduce resource consumption at the same time.

                  

PHP works as an isolated service if you are using PHP-FPM. Web requests are processed over a TCP/IP socket, NGINX only handles HTTP requests, and PHP-FPM interprets PHP code. The use of separate services is very important to improve efficiency.

                                 

                                                     

Possible reasons for 502 Bad Gateway

Let’s take a look at some of the possible reasons for the 502 Bad Gateway.

           

NGINX is not running

If you see error 502, then first you need to check if NGINX is running. To do this, run the following command:

systemctl status nginx

        

If in the response you see that the status is inactive (dead), then you need to start NGINX.

         

PHP-FPM is not running

The next reason for the 502 error is PHP-FPM not running. If you are on Linux, you can check if PHP-FPM is running using the following command:

ps aux | grep php-fpm

     

If no PHP-FPM processes are displayed as a result of this command, then you need to run PHP-FPM to resolve error 502.

     

PHP-FPM timeout expires

If the server takes too long to respond, then a 502 error can be caused by a PHP-FPM timeout. In this case, PHP-FPM closes the connection even before the response is sent, and NGINX returns a 502 error. To fix this problem, you can increase the PHP-FPM timeout, or alternatively, you will have to research your application and figure out why it is not responding for too long.

                         

Firewall blocks requests

A firewall can block communication between the edge servers and the origin server. Some DDoS protection systems or security plugins of your CMS can also block requests from servers.

           

A domain name is not allowed

The domain name is not resolved to the specified IP address or to all IP addresses. In this case, you need to change the DNS server and wait until the changes are propagated and become active globally.

           

Server is unavailable

The original server is down or there is no connection to this server.

             

How to solve 502 error?

Most often, the 502 error occurs due to problems on the side of the website. However, there are times when the cause is in the user’s device. You can check if only you have connection problems or other people also cannot establish a connection to this website. To do this, use one of the tools that check the status of the site and whether it is working or not. For example use one of the following tools or other similar ones: isitdownrightnow.com, downforeveryoneorjustme.com. To learn more about the cause of error 502, you can examine the NGINX log file in your computer (/var/log/nginx/error.log).

                      

As visitor

If you are the only one experiencing connectivity issues, there are several things you can do to resolve the 502 error as a website visitor.

  1. Reload the page in the browser. Often the 502 error is temporary and a simple reboot can help.
  2. Clear browser cache and delete cookies. The browser can save old or corrupted files and data that can cause the 502 error.
  3. Try using a different browser. There may be a problem in the specific browser you are using, and the error will not occur in another browser.
  4. Restart your computer and network equipment. Perhaps this is the problem and after a reboot, the error will disappear.
  5. Check plugins and extensions in your browser. If you use any plugins or extensions and the website works in another browser, then it is possible that they are blocking the connection. Disable all installed plugins and extensions and check the connection again.
  6. Change your DNS server. Changing the DNS server may resolve the 502 error. You can choose Open DNS or Google DNS servers.

                

As developer

In most cases, error 502 occurs for all users due to the fact that the problem is on the website. As a web developer, you can do the following:

  1. Check if your server is available.
  2. Check the firewall logs for unusual crashes.
  3. If you are using Cloudflare, then it returns a 502 Bad Gateway error to your visitors when a certain limit is reached. Try to disable Cloudflare.

                 

Monitoring your system metrics with Hosted Graphite by MetricFire

To identify bottlenecks and troubleshoot errors such as error 502, it is useful to monitor the metrics of your system and the technologies you are using in your application. Hosted Graphite is an open-source platform that provides you with the tools to monitor and analyze your system’s metrics. MetricFire offers hosted Graphite and hosted GrafanaGrafana is a web-based analytics and data visualization application. It allows you to create various customizable dashboards that can include diagrams, graphs, and notifications.

                      

Let's take a look at some of the main features of Grafana:

  1. Dashboard templates. You can design your dashboard in templates as if you were writing code.  With templates, you can use the same design in different environments.
  2. Annotations. You can create logs and comments on your chart manually or automatically.
  3. Custom Plugins. Grafana has the ability to install external plug-ins that allow you to expand its basic functionality.
  4. SQL support. With SQL support, you can easily pull data from a variety of data sources.
  5. Alerts. Grafana can send users notifications about important incidents.

                         

Graphite is a tool for collecting, storing, and processing metrics. You can use Graphite as a data source for Grafana. Grafana comes with an advanced Graphite query editor that allows you to write various queries, add functions, change function parameters, accept complex nested queries using query links, and much more.

                

Hosted Graphite and Grafana let you track your system performance in real-time. MetricFire offers you hosting services for both tools, allowing you to focus on your system’s performance and monitor its metrics in your browser.

                 

To learn more about how to integrate Graphite and Grafana with your system, book a demo with our team or sign up in Hosted Graphite for the free trial today.

                                     

Conclusion

In this article, we looked at what error 502 means, what are the possible reasons for its appearance, and options for fixing it. We also figured out that in order to eliminate errors in your system, it is important to use monitoring tools such as hosted Graphite and Grafana offered by MetricFire.

          

Book a demo with MetricFire experts or sign up for the free trial today and learn more about the features offered by MetricFire.

Hungry for more knowledge?

Related posts