4

[SOLVED] This is a duplicate. Please see this question.

I have a basic Django API working in development when I launch via the runserver command. I am returning a list of objects including the URL of an image in my media folder. In development, this image URL includes the port as shown below. The link works fine when I click it in the browser.

"image_url": "http://0.0.0.0:1337/mediafiles/publisher/sample-image4.jpg",

In production (gunicorn, nginx, docker) everything works the same except the URLs returned by the API do not include the port, so the links are broken. How can I ensure the port is included even in production?

"image_url": "http://0.0.0.0/mediafiles/publisher/sample-image4.jpg",

My guess is it could be an nginx config issue since it works in development server, but I don't really know where the problem is so my searches are not really helping. I'm still quite new to nginx, django and docker.

settings.py

...
STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/mediafiles/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
...

docker-compose.yml

version: '3.7'

services:
  web:
    build: ./app
    command: gunicorn hello_django.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - ./app/:/usr/src/app/
      - static_volume:/usr/src/app/staticfiles
      - media_volume:/usr/src/app/mediafiles
    ports:
      - "8000"
    env_file: ./app/.env
    environment:
      - DB_ENGINE=django.db.backends.postgresql
      - DB_USER
      - DB_PASSWORD
      - DB_HOST=db
      - DB_PORT=5432
      - DATABASE=postgres
    depends_on:
      - db
    networks:
      - backend

  nginx:
    build: ./nginx
    volumes:
      - static_volume:/usr/src/app/staticfiles
      - media_volume:/usr/src/app/mediafiles
    ports:
      - "1337:80"
    depends_on:
      - web
    networks:
      - backend

networks:
  backend:
    driver: bridge

volumes:
  postgres_data:
  static_volume:
  media_volume:

nginx.conf

upstream hello_django {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass http://hello_django;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /staticfiles/ {
        alias /usr/src/app/staticfiles/;
    }

    location /mediafiles/ {
        alias /usr/src/app/mediafiles/;
    }

    location /favicon.ico {
        access_log off;
        log_not_found off;
    }
}
gdg
  • 283
  • 2
  • 9
  • If you manually add the port number into the final url, does the link work? 0.0.0.0 is not a usual production IP address, are you certain of the production link? – Pranab May 05 '19 at 12:15
  • Hi @Pranab yes it works if I manually add the port. I sort of prefer not to add the port at all in production but not really sure how to configure. All the guides add a port. What should I do here? To be more specific about the IP address, it's my production stack running locally i.e. gunicorn, nginx. So it's 0.0.0.0, but won't be on final final server. – gdg May 05 '19 at 18:07
  • Duplicate of this question [SOLVED] https://stackoverflow.com/questions/55915343/how-to-output-the-port-after-the-ip-address-in-a-resource-url-in-django/56254271#56254271. Leaving here for anyone who might be watching this post. – gdg May 22 '19 at 10:06

0 Answers0