Thanks for writing such a detailed thread here! I am not a headers expert, but here is a great thread on debugging a 502 that may be of service to you. It sounds like a similar scenario of working locally, but not once it is deployed to Netlify:
Let us know if following those steps work! If not, I can loop in our Support team as well.