dockerizing-django icon indicating copy to clipboard operation
dockerizing-django copied to clipboard

Absolute URLs without port

Open lorey opened this issue 5 years ago • 0 comments
trafficstars

So I used this to build a dockerized API with Django Rest Framework. Since I develop several projects at once, I use a different port for local development. Turns out that Django or at least Django Rest Framework was not able to produce the right absolute URLs in the browsable API. Instead of localhost:12345/api, I only got localhost/api.

What I tried:

  • setting USE_X_FORWARDED_FOR_HOST and USE_X_FORWARDED_FOR_PORT to True, see https://docs.djangoproject.com/en/3.0/ref/settings/#use-x-forwarded-host

Anyway, what fixed it for me was using $http_host instead of $host in the nginx config.

server {
    listen 80;
    # server_name localhost;
    charset utf-8;

    location /static {
        alias /usr/share/nginx/static;
    }

    location / {
        proxy_pass http://web:8000;
        proxy_set_header Host $http_host;  # $host is without port, $http_host works
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

My issue: https://github.com/lorey/socials-api/issues/4 My configuration: https://github.com/lorey/socials-api/tree/d7f0bca4df5be9c662ede96aac6ae89d17890404

Hope this helps anyone. Let me know if someone finds a better solution or can explain this.

lorey avatar May 06 '20 10:05 lorey