For the mentioned error in your log, this is the first fix.
I observed that if user is logged out of site the changing of templates works, however if user logs in, switching of templates don't work. They still receive 502 gateway error, any idea?
What info in log do you have on these cases?