简体   繁体   English

如何使用 Django、Nginx、Docker 和 Traefik 管理媒体文件?

[英]How to manage to serve media files with Django, Nginx, Docker and Traefik?

I'm trying to upload and persist media files (images, pdfs) in an application with Django.我正在尝试使用 Django 在应用程序中上传和保存媒体文件(图像、pdf)。 It's working fine in development mode, but I'm getting a "404 Not Found The requested URL /mediafiles/images/test.jpg was not found on this server."它在开发模式下工作正常,但我收到“404 Not Found The requested URL /mediafiles/images/test.jpg is not found on this server.” when deployed on a server.当部署在服务器上时。

"Funny" thing to note: I manage to upload files, I see them on my host, and both containers (nginx and webserver), but whenever I try to access them I get the Not Found error. “有趣”的事情要注意:我设法上传文件,我在我的主机和两个容器(nginx 和网络服务器)上看到它们,但是每当我尝试访问它们时,我都会收到 Not Found 错误。

I've read and tries many things without success such as modifying the nginx conf file, playing with the docker-compose file.. would you have any idea where I should look from now on?我已经阅读并尝试了许多没有成功的事情,例如修改 nginx conf 文件,使用 docker-compose 文件..你知道从现在开始我应该看哪里吗?

Here's what I think are pertinent informations:以下是我认为相关的信息:

Server: Ubuntu 16.04服务器:Ubuntu 16.04

Docker-Compose Docker-Compose

services:
  nginx:
    image: nginx
    depends_on:
      - webserver
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - ./static:/usr/share/nginx/html:ro
      - ./mediafiles:/usr/share/nginx/mediafiles:ro
    labels:
      - "traefik.backend=${TRAEFIK_HOST}_static"
      - "traefik.frontend.rule=Host:${TRAEFIK_HOST}; PathPrefixStrip: /static/"
      - "traefik.enable=true"
      - "traefik.port=80"

  webserver:
    image: ${MY_IMAGE}
    volumes:
      - ./static:/tmp/html/static
      - ./mediafiles:/code/server/mediafiles
    labels:
      - "traefik.backend=${TRAEFIK_HOST}"
      - "traefik.frontend.rule=Host:${TRAEFIK_HOST}"
      - "traefik.enable=true"
      - "traefik.port=8000"

volumes:
  mediafiles:

Nginx Config Nginx 配置

server {
listen       80;
server_name  localhost;

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location /mediafiles {
    autoindex on;
    alias /usr/share/nginx/mediafiles/;
}
}

Django Settings.py Django Settings.py

MEDIA_ROOT = '/code/server/mediafiles/'
MEDIA_URL = '/mediafiles/'
STATIC_URL = '/static/'
STATIC_ROOT = '/tmp/html/static/'

Django urls.py Django urls.py

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT,)

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Tell me if there's other information needed.告诉我是否需要其他信息。 And thanks in advance !并提前感谢!

Your issue here is that mediafiles location comes before root one您的问题是mediafiles位置在根目录之前

server {
    listen       80;
    server_name  localhost;

    location /mediafiles {
        autoindex on;
        alias /usr/share/nginx/mediafiles/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM