I just contacted Cloudflare and this is what I have been told:
Thank you for contacting Cloudflare Support. Sorry to hear that you are experiencing some difficulties here. We have run some automated tests on your domain & configuration on Cloudflare based on your ticket, and these are the issues we think will help to resolve your issue.
In most cases, a 502/504 error happens 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.
In this case, and you should contact your hosting provider for further troubleshooting. Reviewing the access & error logs of your web server is a great place to start when these errors occurs as they should:
confirm the response code is being returned by your web server (this is visible in your access logs)
give some indication as to why (this is visible in the error logs, typically)
If, after following the steps above, you feel we can help further could you please provide the following debugging information:
Provide example URL(s) and Ray ID as well as a copy of the output from: http://www.circuitsbm.com/cdn-cgi/trace
Provide two HAR files: one detailing your request with Cloudflare enabled on your website, and the other with Cloudflare temporarily disabled - How do I temporarily deactivate Cloudflare?. These will be helpful in comparing server responses.
You can find a guide on creating HAR files here: How do I generate a HAR file?
Additionally; if this information did not help you or you believe it is inaccurate, please respond to this ticket and we'll take a look for you.