-1

[SOLVED] This is a duplicate. Answer is in this question

(Port wasn't included in resource urls generated by django in production setup, change in nginx .conf fixed it. I was locally testing the production setup on a specific port. Apparently this would not be a problem if the default port is used in production.)


I am (locally) creating a (Dockerized) production setup for my Django project. I am using Django with Gunicorn and NGINX. NGINX handles static and media files, (which works fine as I can access them by manually entering the correct URL). While Django does correctly link to static files, the URLs to media files are wrong.

For media files it skips the port in the URL, while for static files it does not. I tried to remove trailing / and to set MEDIA_URL = 'localhost:1337/media/'. I don't understand why the static URLs are correct and the media URLs are not, as I set them up in the exact same way:

settings.py:

DEBUG = False

STATIC_URL = '/static/'
STATIC_ROOT = 'path/to/static'

MEDIA_URL = '/media/'
MEDIA_ROOT = 'path/to/media'
  • Example of (correct) Django link to static file: http://localhost:1337/static/path/to/file.
  • Example of Django link to uploaded media file: http://localhost/media/path/to/file.
  • Actual location of media file: http://localhost:1337/media/path/to/file.

as requested, nginx.conf:

upstream django_app {
    server web:8000;
}

server {
    listen 80;

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

    location /static/ {
        alias /path/to/static/;
    }

    location /media/ {
        alias /path/to/media/;
    }

}
Duveltje
  • 1
  • 3
  • Slightly confused. Are you saying that you want port 1337 to be in the URL? It looks like you have configured nginx to directly serve static files, so I would assume you would want port 80. – MrName Aug 21 '19 at 19:24
  • Sorry for being unclear. Nginx is running inside a docker container. The container port 80 is mapped to port 1337. So localhost:1337 is where you reach the site. The problem is that django's skips the 1337 in url links to media, while it does work for static files. ie: `localhost/media/path/to/file` instead of `localhost:1337/media/path/to/file` – Duveltje Aug 22 '19 at 08:20

1 Answers1

0

The problem lies with your nginx/docker configuration and not with your django app.

Can you share your nginx config file?

Add the service to docker-compose.prod.yml:

nginx:
  build: ./nginx
  ports:
    - 1337:80
  depends_on:
    - web

Also, set your STATIC_URL and MEDIA_URL, with relative urls, if you are using full url's and change your STATIC_ROOT and MEDIA_ROOT to os.path.join(BASE_DIR, 'static'/'media') instead of specifying a full or relative path manually.

dotslash227
  • 378
  • 2
  • 12
  • Add the service to docker-compose.prod.yml: nginx: build: ./nginx ports: - 1337:80 depends_on: - web – dotslash227 Aug 21 '19 at 15:28
  • Thanks for your answer. The nginx service is already in my docker-compose.prod.yml. Like I mentioned in the question, I can access the media files myself by entering the correct media url. However, the url django assigns to get the media file is incorrect. I tried setting a relative path to MEDIA_ROOT and STATIC_ROOT, but that results in the same problem. – Duveltje Aug 21 '19 at 15:55