realpython / dockerizing-django

https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/
1.34k stars 484 forks source link

Absolute URLs without port #66

Open lorey opened 4 years ago

lorey commented 4 years ago

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:

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.