Try the suggestions in this Community Tip to help you fix Error 502 / Error 504: Bad Gateway / Gateway Timeout.
The 502 / 504 errors are quite similar. Generally, a HTTP 502 / 504 errors occurs because your origin server (e.g. your web host) is returning this code to us, and Cloudflare returns this code in turn to your visitors. They are caused by a problem connecting to an upstream server - meaning your server is trying to initiate a process and this fails to work as expected or times out. In most cases of 502 / 504 errors, back-end servers are not communicating correctly. When this happens, you will see color page with Cloudflare branding and the Error 502 Bad Gateway or Error 504 Gateway Timeout.
Quick Fix Ideas
The Origin Server is not configured to handle the requested domain name at the targeted IP address. Maybe caused when changing DNS records; keep in mind DNS TTL determines how long a record is valid in DNS cache.
The server at the origin is overloaded or unreachable at the time the request was made. The could be due to the server crashing, traffic spikes, or lack of connectivity to the server. Check your origin server logs for clues as to what happened.
An application or service used at your origin with is either timing out or being blocked.
However, on some occasions this error is returned by Cloudflare. This might be before contacting your origin server or if we receive data that is incorrectly compressed from your origin server. In that case, the error page will look specifically like an unbranded black & white text screen.
In a very few cases it is possible Cloudflare is displaying a 502 error page - as above - but in fact a different error occurred in the back-end. You should be able to verify that by generating a HAR file(s), How do I generate a HAR file?
Note that 502s and 504s can be returned by any HTTP-aware device. If you have any load balancers or local proxies in place and do not see errors in the logs of the server that handles that page, check other infrastructure between your origin and Cloudflare.
If You Need More Help
This community of other Cloudflare users may be able to assist you, if not, login to Cloudflare and then contact Cloudflare Support. When you contact support, make sure to include as much of this information as possible: domain name, error messages, screen shots, and/or HAR file(s).
Expert Comments Appreciated
This Community Tip will remain open for input from Community experts and those familiar with this issue. We really appreciate comments that start with words like: “The three things I always try”, or “Do this first” or “In my experience”.
This is a Cloudflare Community Tip, to review other tips click here.