CLYTRIX
All systems operational StatusSupportBlog
Server Guide

How to Fix 502 Bad Gateway Error on Your Website

How to Fix 502 Bad Gateway Error on Your Website

A 502 Bad Gateway error is a common HTTP status code indicating that one server on the internet received an invalid response from another server it accessed while trying to fulfill the request. For web developers and administrators, diagnosing a 502 error requires checking the web server proxy settings, PHP processing engines, and backend logs. In this guide, we will walk through how to fix a 502 Bad Gateway error.

Because the 502 error is a generic gateway response, it can be caused by server-level issues, script timeout limits, or third-party CDN configurations, making a systematic troubleshooting approach essential.

1. What is a 502 Bad Gateway Error?

A 502 Bad Gateway error occurs when your web server (acting as a gateway or proxy) connects to an upstream application engine (such as PHP-FPM or Node.js) to process a request, but the upstream engine returns an invalid response or fails to respond.

It is important to note that a 502 error is typically a server-side issue, not a client-side problem. The visitor's browser connected to the server correctly, but the server encountered an error when trying to retrieve the page content from the backend application.

2. Diagnosing PHP-FPM and Application Crashes

A common cause of 502 errors on PHP hosting environments is PHP-FPM service crashes or execution timeouts. If a PHP script (like a database backup or plugin update) runs too long or exceeds memory limits, the PHP process may terminate unexpectedly.

To troubleshoot this, check your server's error logs (e.g., Nginx or Apache logs). If the logs indicate gateway timeouts, increase your script execution limits in your php.ini file:

max_execution_time = 300
memory_limit = 512M
post_max_size = 128M
upload_max_filesize = 128M

Ensure that the PHP-FPM service is active and has sufficient pool capacity to handle incoming requests.

3. Troubleshooting Proxy and CDN Configurations

If you use a proxy CDN service like Cloudflare, the CDN can return a 502 error if it cannot connect to your origin web server. This can occur if your origin server's firewall blocks Cloudflare's IP range, or if the server goes offline.

To resolve this, verify that your origin server is running and can accept connections. Ensure your firewall whitelist includes Cloudflare's IP ranges, and check that the server's network ports (typically 80 for HTTP and 443 for HTTPS) are open and listening.

Possible Cause Indicator in Logs Recommended Fix
PHP-FPM Crash 'sock connection refused' or 'no process active' Restart PHP-FPM and check memory allocations
Script Execution Timeout 'upstream timed out (110: Connection timed out)' Increase max_execution_time in php.ini
Firewall Blocking CDN Cloudflare 502 page / 'Connection Refused' Whitelist CDN IP ranges in server firewall

Frequently Asked Questions (FAQ)

How do I find my web server's error logs?

In cPanel hosting, you can view error logs via the 'Errors' icon under the Metrics section. If you have root access to the server, you can view the logs in standard server paths (Apache or LiteSpeed error files).

Does a 502 Bad Gateway error affect my SEO rankings?

If the error is resolved quickly, it will have minimal impact on SEO. However, if your site returns 502 errors for several hours or days, search engine spiders will not be able to crawl your pages, which can lead to drops in search rankings.

Can database table locks cause 502 errors?

Yes. If a database query locks a table, subsequent PHP scripts trying to access that table will wait until the lock is released. If the wait time exceeds PHP's maximum execution limit, the script will terminate, causing the web server to return a 502 error.

Ready to Put These Guides Into Practice?

Deploy in under 60 seconds. Plans from $0.69/mo.