# How to Fix Cloudflare Error 502 504 Bad Gateway and Error 526 SSL

> Fix Cloudflare Error 502, 504 Bad Gateway, and Error 526 Invalid SSL with step-by-step PHP-FPM, Nginx, firewall, and Full Strict SSL solutions.

- **Publisher**: Egy Monitor (https://egymonitor.com/en)
- **Original URL**: [https://egymonitor.com/en/posts/how-to-fix-cloudflare-error-502-504-526-ssl-guide](https://egymonitor.com/en/posts/how-to-fix-cloudflare-error-502-504-526-ssl-guide)
- **Category**: [Web Hosting & Dev](https://egymonitor.com/en/categories/web-hosting-dev)
- **Author**: Abdelrahman Shawqi
- **Published Date**: 2026-11-26 20:59
- **Keywords**: Cloudflare Error 502 504 526, fix Cloudflare 502 Bad Gateway, fix Cloudflare 526 invalid SSL, Cloudflare Full Strict SSL, Nginx PHP-FPM timeout fix, website troubleshooting

---

Cloudflare Error 502 and 504 occur when your origin web server fails to return a valid or timely response to Cloudflare's edge, while Error 526 happens when Cloudflare's Full (Strict) SSL mode rejects an expired, self-signed, or mismatched SSL certificate on your host. Fixing them requires tuning PHP-FPM/Nginx timeouts, allowlisting Cloudflare IPs, and installing a valid origin certificate.

## Understanding Cloudflare 502 504 and 526 Errors at a Glance

Seeing a red Cloudflare error screen instantly drops organic traffic and breaks checkout flows, yet each HTTP 5xx code points to a specific breakdown between Cloudflare's edge proxy and your hosting server. Before editing server configs or changing SSL settings, check which status code appears and whether the page shows Cloudflare's branded diagram or a plain unbranded error screen.

Error Code
Core Meaning
Most Common Origin Cause
Primary Fix

Error 502
Bad Gateway
Crashed PHP-FPM/Nginx process, broken gzip header, or dropped upstream socket
Restart PHP-FPM/Nginx, check error logs, verify gzip headers

Error 504
Gateway Timeout
Slow database query, maxed-out PHP workers, or firewall blocking Cloudflare IPs
Raise origin timeouts, optimize slow queries, allowlist Cloudflare IP ranges

Error 526
Invalid SSL Certificate
Expired, self-signed, or missing domain SAN on origin cert under Full (Strict)
Install valid CA cert or Cloudflare Origin CA cert covering root and www

## How to Fix Cloudflare Error 502 and 504 Bad Gateway

As detailed in the official [Cloudflare Error 502 and 504 troubleshooting docs](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-502-504/), these status codes almost always originate on your web server rather than inside Cloudflare's edge network. When an unbranded 502 or 504 appears, it can also point to a compression mismatch where the origin server sends gzip-encoded content without updating the `Content-Length` header or serves corrupted compressed output.

### Step-by-Step Origin Server Troubleshooting for 502 and 504

- **Check PHP-FPM and Web Server Status:** SSH into your VPS or open your hosting control panel and confirm that Nginx, Apache, and PHP-FPM services are active without out-of-memory kills or socket crashes.

- **Inspect Origin Error Logs:** Review `/var/log/nginx/error.log`, Apache error logs, or your host's PHP log for entries like `upstream prematurely closed connection`, `recv() failed (104: Connection reset by peer)`, or `upstream timed out (110: Connection timed out)`.

- **Increase Worker Pool and Timeouts:** In PHP-FPM, increase `pm.max_children` if concurrent visitors saturate all PHP workers. In Nginx, verify `fastcgi_read_timeout` and `proxy_read_timeout` sit around 60 to 90 seconds so long-running scripts finish cleanly before Cloudflare's 100-second edge timeout triggers Error 504.

- **Allowlist Official Cloudflare IP Ranges:** Confirm that UFW, CSF, iptables, Fail2ban, or ModSecurity on your server is not rate-limiting or dropping Cloudflare's published IPv4 and IPv6 proxy ranges. Because every visitor arrives through Cloudflare's IPs, strict per-IP firewall rules often ban Cloudflare itself during traffic spikes.

- **Test Disabling Origin Gzip Compression:** If you encounter an unbranded 502 error, temporarily turn off gzip compression on your origin web server to verify whether malformed compressed headers are breaking the upstream response.

#### Diagnosing Cloudflare Edge vs Origin Bottlenecks

Visit `https://yourdomain.com/cdn-cgi/trace` to see which Cloudflare data center is handling your request. Next, test your server directly by temporarily mapping your domain to your origin IP in your computer's local hosts file. If the site loads fast directly from the origin while throwing 502 or 504 through Cloudflare, focus immediately on firewall allowlists, Keep-Alive timeouts, and origin response headers.

> 
Never leave Cloudflare paused or switched to DNS-Only grey cloud as a permanent workaround for 502 or 504 errors. Exposing your origin IP strips away DDoS protection and leaves an underlying PHP worker or database bottleneck waiting to crash again under load.

## How to Fix Cloudflare Error 526 Invalid SSL Certificate

According to the official [Cloudflare Error 526 documentation](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-526/), Error 526 triggers strictly when your Cloudflare SSL/TLS encryption mode is set to **Full (Strict)** and Cloudflare cannot cryptographically validate the SSL certificate installed on your origin web server.

### Why Full Strict Rejects Your Origin Certificate

- **Expired Certificate:** Your Let's Encrypt or commercial SSL certificate failed to auto-renew on the host because a firewall rule or redirect blocked the renewal check.

- **Self-Signed Certificate:** Your server presents a default self-signed certificate that is not signed by a trusted public Certificate Authority or Cloudflare's Origin CA.

- **Missing Hostname SAN:** The certificate covers `example.com` but omits `www.example.com` or a proxied subdomain inside its Subject Alternative Name (SAN) list.

- **Incomplete Certificate Chain:** Nginx or Apache serves only the leaf certificate without bundling the intermediate CA chain required for strict verification.

Full Strict vs Full SSL Mode Explained
Switching temporarily from **Full (Strict)** to **Full** inside the Cloudflare dashboard under SSL/TLS Overview restores site access right away because Full mode encrypts traffic without verifying certificate validity. Treat that toggle strictly as a five-minute emergency bridge while you install a valid certificate. Avoid switching to Flexible mode if your origin enforces HTTPS redirects, as doing so creates an immediate `ERR_TOO_MANY_REDIRECTS` loop.

### Installing a Free 15-Year Cloudflare Origin CA Certificate

If Let's Encrypt HTTP-01 renewals keep breaking behind proxy or redirect rules, generate a free Cloudflare Origin CA certificate directly in your Cloudflare dashboard under **SSL/TLS > Origin Server**. Add both `yourdomain.com` and `*.yourdomain.com` as hostnames, select a 15-year validity period, paste the generated PEM certificate and private key into your Nginx, Apache, or cPanel SSL manager, reload your web server, and switch SSL/TLS encryption back to **Full (Strict)**.

Quick Verification Command After Fixing SSL
Test your origin server directly from a terminal using `curl -Iv --resolve yourdomain.com:443:YOUR_SERVER_IP https://yourdomain.com` to confirm the host presents the renewed certificate, matches every SAN hostname, and returns HTTP 200 without TLS handshake warnings.

### Frequently Asked Questions

#### What is the difference between Cloudflare Error 502 and Error 504?

Error 502 means your origin server returned an invalid, malformed, or prematurely closed response to Cloudflare, whereas Error 504 means your origin server took too long to reply and exceeded Cloudflare's connection timeout.

#### Does switching from Full Strict to Full fix Error 526 permanently?

Switching to Full clears the Error 526 screen immediately, but it turns off origin certificate validation. You should always renew your Let's Encrypt certificate or install a Cloudflare Origin CA certificate and return to Full (Strict) for verified end-to-end encryption.

#### Can a WordPress plugin cause Cloudflare Error 502 or 504?

Yes. Heavy backup plugins, unoptimized WooCommerce queries, or broken PHP hooks can exhaust PHP-FPM workers and trigger 502 or 504 errors until you disable the offending plugin or increase server memory and worker limits.

#### How long does Cloudflare wait before throwing Error 504?

Cloudflare waits up to 100 seconds for an HTTP response from your origin server on standard plans before terminating the connection and displaying a 504 Gateway Timeout error.
