Proxy - logged in user redirect to incorrect location

When using ERPNext through a proxy (e.g. erpnext.mycompany.com proxies to http://ec2-xxx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com/):

It seems to come down to how frappe.Redirect is handled. I can’t find whether and how it’s possible to change this to a relative redirect instead of a full path, or alternatively whether it’s possible for it to use the correct url in the full redirect.

I’m using Apache as the proxy. Adding ProxyPreserveHost On in the Apache config files seems to have resolved the problem, but doesn’t account for the difference in behaviour between redirect at login and redirect when already logged in.