How to Fix the 502 Bad Gateway Error: Complete Server & CDN Solutions
Google AdSense In-Article Display Placement
Publisher ID: ca-pub-XXXXXXXXXXXXXXXX (Configured in config.php)The HTTP status code 502 Bad Gateway indicates that an edge server or reverse proxy (like Cloudflare, Nginx, or LiteSpeed) received an invalid response from the upstream origin application server.
1) Restart PHP-FPM or Apache on your origin server (e.g. "systemctl restart php8.2-fpm"). 2) Check if your server CPU or RAM is at 100% load. 3) If using Cloudflare, verify your origin server firewall allows all Cloudflare IP ranges. 4) Inspect Nginx/Apache error logs for upstream connection refused.
📋 Guide Table of Contents
1. Problem Diagnosis & Symptoms
When the backend script crashes or fails to respond within the gateway timeout window, the reverse proxy returns 502.
2. Primary Root Causes
- Cause: PHP-FPM worker pool exhausted or crashed.
- Cause: Server overloaded by traffic.
3. Step-by-Step Diagnostic Fixes
Restart PHP-FPM Daemon
Restart workers in SSH:
sudo systemctl restart php8.2-fpm
sudo systemctl restart nginx
4. How to Verify the Resolution
- Refresh the page; confirm status returns 200 OK.
5. Diagnostic Troubleshooting Matrix
| Symptom Observed | Root Cause | Recommended Action |
|---|---|---|
| 502 Bad Gateway: connect() failed (111: Connection refused) | PHP-FPM socket not active or wrong path in Nginx config | Start PHP-FPM service and verify fastcgi_pass socket path |
6. Critical Mistakes to Avoid
- ❌ Assuming 502 is a browser-side client issue.
7. Frequently Asked Questions
❓ What is the difference between 502 and 504?
502 means the upstream server sent an invalid/empty response. 504 means the upstream server took too long and timed out.
Official Technical References
Google AdSense In-Article Matched Content Unit