Seeing a 502 Bad Gateway error on Nginx can be frustrating, especially when your WordPress site suddenly stops responding. This error usually happens when PHP-FPM crashes, times out, or fails to process requests. The good news is that the fix is straightforward. In this guide, you’ll learn how to diagnose the root cause and restore your Ubuntu or Virtualmin server in just a few steps.
A 502 Bad Gateway error on an Nginx server usually appears when the backend process — typically PHP-FPM — fails to respond. This is one of the most common issues for WordPress websites hosted on unmanaged VPS servers using Ubuntu, Nginx, and Virtualmin.
The good news is that a 502 error is almost always fixable with a few targeted checks. Below, we’ll walk through the most reliable ways to diagnose and resolve the issue.
🔍 Why 502 Happens on Nginx
Nginx throws a 502 error when it cannot communicate with PHP-FPM. Common reasons include:
PHP-FPM crashed or stopped running
PHP-FPM is using the wrong socket or port
PHP version mismatch (e.g., Nginx expects php8.1-fpm but server runs php8.2-fpm)
Script timeout due to heavy plugins or slow queries
Memory exhaustion
Wrong configuration inside Virtualmin
File permission issues
Let’s fix it step-by-step.
🛠️ Step 1: Check if PHP-FPM is Running
Run the correct command depending on your PHP version:
systemctl status php8.1-fpm
If you see inactive or failed, restart it:
systemctl restart php8.1-fpm
For PHP 8.2 servers:
systemctl restart php8.2-fpm
If PHP-FPM starts without errors, refresh your website.
If not, continue below.
🛠️ Step 2: Restart Nginx
Many 502 errors are resolved simply by restarting both processes:
systemctl restart nginx
systemctl restart php8.1-fpm
Check again in your browser.
🛠️ Step 3: Check PHP-FPM Socket or Port
Open your Nginx site configuration:
nano /etc/nginx/sites-available/yourdomain.com.conf
Look for:
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
If your server uses PHP 8.2, the socket should be:
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
Mismatch = instant 502 error.
You can list available sockets:
ls /var/run/php/
Update the config if needed, then test and reload:
nginx -t
systemctl reload nginx
🛠️ Step 4: Check PHP-FPM Error Logs
Use this command:
tail -n 50 /var/log/php8.1-fpm.log
Look for:
“Out of memory”
“Child exited with signal”
“Slow script”
“Cannot allocate memory”
“Pool failed”
If you see memory issues, increase your limits:
nano /etc/php/8.1/fpm/php.ini
Update:
memory_limit = 512M
Restart PHP-FPM:
systemctl restart php8.1-fpm
🛠️ Step 5: Increase PHP-FPM Timeout
Slow PHP scripts can cause 502 errors.
nano /etc/php/8.1/fpm/pool.d/www.conf
Increase:
request_terminate_timeout = 300
In Nginx:
fastcgi_read_timeout 300;
Reload:
systemctl reload nginx
🛠️ Step 6: Virtualmin Users — Fix Execution Mode
In Virtualmin:
Web Configuration → PHP Options → PHP script execution mode
Set to:
✔ FPM (recommended)
or
✔ CGI (fallback option)
Switching between these can resolve socket mismatches.

🟢 Final Test
Reload both services:
systemctl restart nginx
systemctl restart php8.1-fpm
Your site should now load without the 502 error.
🎉 Conclusion
A 502 Bad Gateway error on Nginx usually comes down to a communication issue with PHP-FPM — often caused by a crash, version mismatch, or timeout. By checking service status, verifying sockets, analyzing logs, and adjusting limits, you can reliably restore your site.
If this still doesn’t fix it, you may be dealing with:
Broken plugin
Heavy theme function
Corrupted WordPress installation
MySQL overload
Just drop the error details in comments and I can help diagnose the next layer.



