How to Fix HTTP 502 Bad Gateway and 504 Gateway Timeout Errors

54 views
Short answer: Increase max_execution_time to 300 under cPanel Select PHP Version > Options, terminate stuck backend PHP loops, and bypass proxy routing to diagnose origin server response.

Troubleshoot upstream communication timeouts between LiteSpeed, Cloudflare CDN, and backend PHP-FPM processes.

Before you start: Back up your site files and database and download the backup to your own computer. How to take a full backup

Quick reference

HTTP 502 Bad Gateway (Upstream server returned an invalid response)
HTTP 504 Gateway Timeout (Upstream server took too long to complete request)
Primary Culprit PHP execution timeout or hung external API call

Quick diagnosis

Scenario & Suspected Cause Immediate Action & Remedy
PHP execution timeout exceeded Increase max_execution_time to 300 seconds under cPanel Select PHP Version > Options.
PHP backend crashed or stopped responding Review error_log for unhandled loops or blocked outbound third-party API calls.
Cloudflare edge proxy timeout Temporarily toggle off Cloudflare orange-cloud proxy to test direct server communication.
Database table lock deadlock Inspect MySQL processlist in phpMyAdmin and repair locked database tables.

Steps

  1. Differentiate Cloudflare vs Origin Timeout:
    • If the error page features a Cloudflare banner, your origin Sib Host server took longer than 100 seconds to respond.
    • If the error page is plain text, LiteSpeed or PHP-FPM timed out locally.
  2. Increase PHP max_execution_time: In cPanel > Select PHP Version > Options, increase max_execution_time to 300 seconds.
  3. Restart PHP Processes: Switch your PHP version slightly (e.g. from 8.4 to 8.3 and back to 8.4) in PHP Selector to terminate hung background worker threads.

Troubleshooting and tips

  • Slow External API Calls: If a shipping plugin, payment gateway, or analytics tracker is unreachable, PHP waits until timeout before aborting. Temporarily disable external integrations to verify.
Need more help? If you run into an error while following these steps, open a ticket with technical support and our team will look into it. Open a ticket
Was this article helpful?
No, I need more help
Didn't find your answer?
Under 3 minutes
Submit Support Ticket