

















Heya,
When you get a 502 Bad Gateway error, the problem is usually not Nginx itself. Most of the time, Nginx is working, but it cannot get a valid response from the backend service it depends on, which is usually PHP-FPM for WordPress.
For a WordPress site on Nginx + Ubuntu 22.04, the most common causes are:
To troubleshoot it, first check the Nginx error log:
sudo tail -f /var/log/nginx/error.log
Or review recent lines with:
sudo tail -n 50 /var/log/nginx/error.log
That log usually tells you whether Nginx failed to connect to PHP-FPM, hit a timeout, or found a bad upstream definition.
Since WordPress runs through PHP, the next important step is to check PHP-FPM logs and status.
First, see which PHP version is installed:
php -v
Then check the PHP-FPM service:
sudo systemctl status php8.1-fpm
On Ubuntu 22.04, the PHP-FPM log is often here:
sudo tail -n 50 /var/log/php8.1-fpm.log
You can also inspect the system journal:
sudo journalctl -u php8.1-fpm -n 50 --no-pager
If PHP-FPM is stopped or failing, restart it:
sudo systemctl restart php8.1-fpm
Also confirm that Nginx is pointing to the correct PHP socket. In your Nginx site config, look for something like:
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
If Nginx points to the wrong PHP version or a missing socket, you can get intermittent 502 errors immediately.
After that, check WordPress/application-level logs. For WordPress, enable debugging in wp-config.php:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
Then review:
tail -n 50 /path/to/wordpress/wp-content/debug.log
That helps catch plugin, theme, and PHP fatal errors that do not always appear clearly in the browser.
It is also worth checking whether the server is running out of memory:
free -h
dmesg -T | grep -i -E 'killed process|out of memory'
If PHP-FPM workers are being killed by the OOM killer, the site may alternate between hanging and returning 502.
A practical fix checklist would be:
nginx/error.logphp-fpm service status and logsfastcgi_pass socket/path in Nginxsudo nginx -t
sudo systemctl reload nginx
sudo systemctl restart php8.1-fpm
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。