Possible causes and Solutions of Nginx 502 Bad Gateway errors in LNMP
Sometimes php-fpm will stop accidentally, so the first thing you need to check is the status of php-fpm. If it is down, you need to enable it.
Systemctl status php-fpm
It's best to set up boot: systemctl enable php-fpm