Cloudflare 502 and 504 error

May I ask have you tried checking with the steps from below article?:

https://support.cloudflare.com/hc/en-us/articles/115003011431-Troubleshooting-Cloudflare-5XX-errors#502504error