[Fixed]-How do i configure the django rest framework pagination url

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']
        ...

See Real life usage of the X-Forwarded-Host header?

๐Ÿ‘คSina Khelil

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.

Leave a comment