502 Error when enabled cloudflare

Bad gateway usually indicates Cloudflare can’t reach your origin server. There’s likely some firewall blocking Cloudflare IPs (maybe your server host? I imagine some hosts don’t like Cloudflare’s free SSL).

Check with your server host and see if they can provide assistance, also a good idea to open a CF support ticket and they can look into why CF is getting the error.