Website Shows 502 or 504 Error
Short answer502 Bad Gateway means the web server/proxy got an invalid response from the backend (PHP-FPM, Node, Gunicorn, etc.). 504 Gateway Timeout means the backend took too long. Start with Nginx/Apache error logs, verify the upstream service is running, then check timeouts, RAM, and disk.
Overview
Both errors usually come from your reverse proxy stack inside the guest OS, not VPSGrid hypervisor issues. If the whole VPS is unreachable, see VPS Is Running but Website Is Down. If the server is slow under load, see VPS Is Slow: How to Diagnose.
502 vs 504 — quick difference
| Code | Meaning | Typical cause |
|---|---|---|
| 502 | Bad Gateway — proxy could not get a valid upstream response | Backend stopped, wrong socket/port, crash, immediate failure |
| 504 | Gateway Timeout — proxy waited too long | Slow app/DB query, low timeouts, worker exhaustion under load |
1) Reproduce and read logs
# Nginx
sudo tail -100 /var/log/nginx/error.log
# Apache
sudo tail -100 /var/log/apache2/error.log # Debian/Ubuntu
sudo tail -100 /var/log/httpd/error_log # Alma/RockyNote the timestamp and exact message (connect() failed, upstream timed out, Primary script unknown, etc.).
2) Verify the upstream is running
PHP-FPM (WordPress/LEMP):
sudo systemctl status php8.3-fpm # version may differ
ls /run/php/
grep fastcgi_pass /etc/nginx/sites-enabled/*Node.js / PM2:
pm2 status
curl -I http://127.0.0.1:3000/Python Gunicorn:
sudo systemctl status gunicorn
curl -I http://127.0.0.1:8000/Runtime install: How to Install Node.js on a Linux VPS.
3) Fix common 502 causes
- Start/restart the upstream service (
systemctl restart php8.3-fpm,pm2 restart all) - Fix socket mismatch after PHP upgrade (e.g. config still points to
php8.2-fpm.sock) - Confirm
proxy_pass/fastcgi_passtargets localhost or the correct port - Check disk full (
df -h) and OOM kills (dmesg | tail -20 | grep -i kill)
4) Fix common 504 causes
- Raise proxy timeouts in Nginx/Apache if requests are legitimately long
- Optimize slow database queries or heavy API calls
- Increase worker counts only if RAM allows — otherwise upgrade the plan
# Nginx examples (inside location/server block)
proxy_connect_timeout 60s;
proxy_read_timeout 120s;
fastcgi_read_timeout 120s;5) Problem → action matrix
| Log / symptom | Likely issue | Action |
|---|---|---|
connect() failed (111) | Upstream not listening | Start service; verify port/socket |
upstream timed out | Slow backend or low timeout | Optimize app; raise read timeout |
Primary script unknown | PHP-FPM path/root wrong | Fix document root and SCRIPT_FILENAME |
| 502 only under traffic | Worker/RAM exhaustion | Tune pm.max_children / Gunicorn workers or upgrade |
| 502 after Certbot/SSL change | Broken vhost upstream block | Diff SSL vhost; restore fastcgi/proxy directives |
6) Apache reverse proxy note
sudo apache2ctl -M | grep proxy
sudo tail -50 /var/log/apache2/error.logEnsure proxy modules are enabled and ProxyPass targets a running backend.
Frequently Asked Questions
Is 502/504 a VPSGrid network outage?
Usually no. These codes come from your web server on the VPS. Total outage symptoms are different from gateway errors on one site.
Why intermittent 502/504?
Traffic spikes, timeouts, or OOM kills often cause bursts. Check resources and upstream worker limits.
502 after SSL install?
Certbot may rewrite vhosts. Compare config before/after and verify PHP-FPM socket paths still match.
Related Articles
Need More Help?
Open a ticket at billing.vpsgrid.net with error log excerpts, upstream stack (PHP/Node/Python), and whether the issue is 502 or 504.
¿Le ha resultado útil esta respuesta?