4
votes

Firstly apologies if this is a duplicate but i have not found a solution through similar posts shown in SO

I have a Docker Django image which is using nginx and gunicorn.

Gunicorn script:

exec /var/www/venv/bin/gunicorn wsgi:application \
--bind 0.0.0.0:8001 \
--access-logfile /var/log/gunicorn/access.log \
--error-logfile /var/log/gunicorn/error.log

Nginx config:

server {

    server_name 172.0.0.1;

    access_log off;

    location / {
            proxy_pass http://127.0.0.1:8001;
            proxy_set_header Host $host:$server_port;
    }

    location /static/ {
        autoindex on;
        alias /var/www/django/assets/;
        expires 7d;
    }


}

I am exposing port 80 and mapping to 49260.

When browsing to the docker host external ip including the port the site is published and serves the static files.

http://xxx.xx.xx.xxx:49260/

The issue is when i navigate to any other page in the django site, the mapped port is dropped from the URL which is then picked up by the host server ngnix config.

What i am trying to achieve is maintain the port in the URL which i can later reverse proxy from the host server.

Any advice would be really appreciated.

1

1 Answers

2
votes

The answer was adding:

proxy_set_header Host $http_host;

to the nginx conf which prints hostname:portnumber

See serverfault.com link here: Original thread