'Nginx status when "502 Bad Gateway"

I usually can open this page (http://x.xxx.xxx.xx/page.php) but sometimes it shows "502 Bad Gateway" and I have to restart PHP-FPM using below command.

sudo systemctl restart php7.2-fpm.service

What's the issue as nginx and PHP-FPM seem fine?

nginx status: OK

I have set /etc/nginx/sites-available/egent.conf as below so that I can check nginx status by simply opening http://x.xxx.xxx.xx/nginx_status on the browser.

location /nginx_status {
    stub_status on;
    allow all;
}

It seems like there's no problem with nginx.

Active connections: 7
server accepts handled requests
1313 1313 1331
Reading: 0 Writing: 6 Waiting: 1

nginx status: OK

In the terminal when I execute systemctl status nginx, it seems nginx is running.

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2022-04-05 23:34:38 JST; 20h ago
 Main PID: 13763 (nginx)
    Tasks: 3
   Memory: 6.3M
      CPU: 6.985s
   CGroup: /system.slice/nginx.service
           ├─ 7220 nginx: worker process                           
           ├─ 7221 nginx: worker process                           
           └─13763 nginx: master process /usr/sbin/nginx -g daemon on; master_process on

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

PHP-FPM: OK

I also execute systemctl status ps aux | grep php-fpm, it seems PHP-FPM is running.

www-data 13197  0.0  1.0 553688 42500 ?        S    16:15   0:00 php-fpm: pool www
root     13791  0.0  0.7 465860 30412 ?        Ss   Apr05   0:04 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
www-data 15216  0.0  0.9 549612 37192 ?        S    17:35   0:00 php-fpm: pool www
www-data 16136  0.0  0.9 551752 39988 ?        S    18:15   0:00 php-fpm: pool www
www-data 16565  0.0  1.0 555784 42800 ?        S    18:35   0:00 php-fpm: pool www
...
ubuntu   18371  0.0  0.0  15632   868 pts/8    S+   19:45   0:00 grep php-fpm


Solution 1:[1]

can you show fastcgi_pass in config nginx? you can to try:

  fastcgi_pass 127.0.0.1:9000;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ned