9๐
Set USE_X_FORWARDED_HOST
in your settings to True
and make sure you pass it along using your web server(proxy) as well.
When django does build_absolute_uri()
it calls get_host()
โ see below in django.http.request:
def get_host(self):
"""Returns the HTTP host using the environment or request headers."""
# We try three options, in order of decreasing preference.
if settings.USE_X_FORWARDED_HOST and (
'HTTP_X_FORWARDED_HOST' in self.META):
host = self.META['HTTP_X_FORWARDED_HOST']
...
11๐
It sounds like your Host
header is not being set properly, which would be an issue in your nginx configuration. The issue is that your Host
header that is being sent includes the port number, so Django is including the port number when building out urls. This will cause future issues with CSRF, because CSRF checks do strict port checking when you are not debugging.
This is known to cause issues with SSL for similar reasons.
You can fix this by setting the Host
header within Nginx to not include the proxied port.
proxy_set_header Host $http_host;
Note that I used the $http_host
variable instead of $host
or $host:$server_port
. This will ensure that Django will still respect CSRF requests on non-standard ports, while still giving you the correct absolute urls.
- How to display "x days ago" type time using Humanize in Django template?
- How to set initial values for a ModelForm when instance is also given
- In Django, how to display times with lowercase am/pm in templates?