Context
We have a Docker container running Django on Azure. The container is served through a Azure Application Gateway.
Because Django is behind a a proxy you should use use the USE_X_FORWARDED_HOST
setting to let Django fetch the hostname from that header. See this link for the documentation:
https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-USE_X_FORWARDED_HOST
Unfortunartly the Azure Application Gateway cannot provide the X-Forwarded-Host
header, it does however provide a X-Original-Host
.
According to this medium the X-Forwarded-Host
header must be set at the public internet facing proxy. So I can not set it on the Nginx proxy running inside the docker container. I have tried with the settings below on Nginx, i see the X-Forwarded-Host
is set but it doesn't get picked up by Django.
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'authorization, content-type';
add_header 'X-Forwarded-Host' 'www.mydomain.com';
proxy_set_header Host $host;
proxy_redirect off;
proxy_pass http://backend_api$request_uri;
}
And also tried with proxy_set_header X-Forwarded-Host www.mydomain.com
. I see the header is set in the response, but Django doesn't use it for the absolute urls.
Question
How can i let django use the X-Orignal-Host
instead of the X-Forwarded-Host
header? or hardcode the hostname in another way? Preferably I do not want to use the django.contrib.sites
module, because it is build for multisite content management.