Diagnosing 502 Bad Gateway Error

Last updated: July 6th 2022

Introduction

This article focuses on resolving a common issues many users face when running a website or a web app, namely encountering a 502 Bad Gateway error.

Prerequisites

Common Scenarios

A 502 Bad Gateway essentially means that the backend of your web app or website is not running. Most commonly, you would encounter this error, on our servers, when you have PHP-FPM failing to start or when Nginx couldn’t proxy to your web app (NodeJS) running on localhost.

On our LEMP/LAMP stacks

While you are on our LEMP/LAMP stacks, things are perfectly configured. So it is highly unlikely you will see the 502 Bad Gateway Error. This error comes up when PHP-FPM is not ready to process PHP scripts.

Check the PHP-FPM by executing the below command.

$ sudo systemctl status phpX.X-fpm

Replace the X.X with the version of the LEMP/LAMP stack your server is using. For example, if you are using LEMP 8.1, then it should be php8.1-fpm.

● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Sun 2022-07-03 13:46:59 UTC; 17s ago
       Docs: man:php-fpm8.1(8)
    Process: 282 ExecStart=/usr/sbin/php-fpm8.1 --nodaemonize --fpm-config /etc/php/8.1/fpm/php-fpm.conf (code=exited, status=0/SUCCESS)
    Process: 2219 ExecStopPost=/usr/lib/php/php-fpm-socket-helper remove /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exited, status=0/SUCCESS)
   Main PID: 282 (code=exited, status=0/SUCCESS)
     Status: "Processes active: 0, idle: 21, Requests: 19, slow: 0, Traffic: 0req/sec"

Jul 03 13:43:55 nginxtest systemd[1]: Starting The PHP 8.1 FastCGI Process Manager...
Jul 03 13:43:55 nginxtest systemd[1]: Started The PHP 8.1 FastCGI Process Manager.
Jul 03 13:46:59 nginxtest systemd[1]: Stopping The PHP 8.1 FastCGI Process Manager...
Jul 03 13:46:59 nginxtest systemd[1]: php8.1-fpm.service: Succeeded.
Jul 03 13:46:59 nginxtest systemd[1]: Stopped The PHP 8.1 FastCGI Process Manager.

The above text shows that PHP-FPM is not running/inactive. If you see similar output try to restart using the command

$ sudo systemctl restart phpX.X-fpm

Now hopefully the gateway error will disappear if PHP-FPM starts without any errors.

Proxying (proxy_pass) with Nginx

There is a use-case where you would use Nginx to proxy pass a Node app for example that is running on localhost. When, for some reason, the Node app is not running and or listening on the specified port Nginx throws the gateway error.

Essentially, another way to understand 502 Bad Gateway is by understanding that this means that your webserver cannot reach whatever is running "behind" it and is responsible for further processing of the http request.

Below is a sample Node app running on port 3000

const http = require('http');
const { Http2ServerRequest } = require('http2');

const server = http.createServer((req, res) => {
    //printing req
    console.log(req.url);
    res.end("This is a simple http server");
})

server.listen(3000, '127.0.0.1', () =>{
    console.log("Server listening on port 3000");

The corresponding Nginx config to proxy the node app

server{
    listen 80;
    server_name example.com;
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        # location /overview {
        #     proxy_pass http://127.0.0.1:3000$request_uri;
        #     proxy_redirect off;
        # }
    }
}

When you hit example.com and see the gateway error, that means the Node app is not running or maybe you’ve specified the wrong port. In this case, please make sure your Node app is running, on the correct port and nothing in your iptables is blocking communication with your app.

Conclusion

In this article, we have outlined instructions on how to diagnose the 502 Bad Gateway Error.

If the issue still persists, or if you see other errors, please contact Webdock Support. We’ll be happy to assist you.

We use cookies. Please see our Privacy Policy.