Low Orbit Flux Logo 2 D

NGINX - 502 bad gateway

If you are running NGINX and you are seeing the error “502 bad gateway” it means that your webserver is having trouble talking to another server.

Oftentimes this can mean that NGINX is having trouble talking to PHP-FPM.

Here are three things you might want to try before any other troubleshooting. Validate the NGINX configuration, restart PHP-FPM, and restart NGINX.


sudo nginx -t
sudo systemctl restart php7.4-fpm
sudo systemctl reload nginx

Example Issue - 502 bad gateway

When trying to access this test PHP page I was receiving a “502 bad gateway” error.

http://192.168.3.22/test2.php

The page is a basic PHP hello world page that looks like this:


<html>
 <head>
  <title>Test</title>
 </head>
 <body>
 <?php echo 'Hello World'; ?>
 </body>
</html>

I was seeing the following errors in the NGINX logs:


user1@zippy-zap:/var/www/html$ cat /var/log/nginx/error.log 
2021/11/28 16:43:04 [error] 781614#781614: *3 open() "/var/www/html/test.html" failed (13: Permission denied), client: 192.168.3.22, server: _, request: "GET /test.html HTTP/1.1", host: "192.168.3.22"
2021/11/28 19:46:00 [notice] 823552#823552: signal process started
2021/11/28 19:47:24 [crit] 823553#823553: *4 connect() to unix:/run/php/php7.0-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.3.22, server: _, request: "GET /test2.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "192.168.3.22"
2021/11/28 20:07:13 [crit] 823553#823553: *6 connect() to unix:/run/php/php7.0-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.3.22, server: _, request: "GET /test2.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "192.168.3.22"
2021/11/28 20:07:14 [crit] 823553#823553: *6 connect() to unix:/run/php/php7.0-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.3.22, server: _, request: "GET /test2.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "192.168.3.22"
2021/11/28 20:07:15 [crit] 823553#823553: *6 connect() to unix:/run/php/php7.0-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.3.22, server: _, request: "GET /test2.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "192.168.3.22"
user1@zippy-zap:/var/www/html$ 

I checked that PHP-FPM was running like this:


systemctl list-units |grep -i fpm

I noticed that I was using version 7.4 of while my configuration file is setup to point to the the socket file for version 7.0.


php7.4-fpm.service              

I changed my configuration file from this:


/etc/nginx/sites-available/default
..
..
        location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/run/php/php7.0-fpm.sock;
       }

To this:


/etc/nginx/sites-available/default

..
..
       location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }

After updating the version in my configuration file I restarted NGINX like this:


sudo systemctl reload nginx