简体   繁体   English

如何使用 docker 和 nginx 服务 django 媒体文件?

[英]How to serve django media files with docker and nginx?

My setup for dockerizing & serving the Django app is a bit different when it comes to Nginx so the solutions I found didn't help.当涉及到 Nginx 时,我为 Django 应用程序提供 dockerizing 和服务的设置有点不同,所以我找到的解决方案没有帮助。

I have dockerized the django app and its working fine except the media files.我已经对 django 应用程序进行了 docker 化,它的工作正常,但媒体文件除外。 I am not using nginx inside the container but on the actual server.我没有在容器内使用 nginx,而是在实际服务器上使用。 All the requests even static files are served perfectly except media files.除媒体文件外,所有请求甚至 static 个文件都得到完美处理。 Here is settings for static and media:这是 static 和媒体的设置:

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

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


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

Here is my docker-compose file:这是我的 docker-compose 文件:

version: "3"
services:
  backend:
    image: gitrepo:latest
    #image: 731d5d7e9296
    ports:
      - 8400:8000
    links:
      - database
    command: bash -c "python manage.py migrate
      && python manage.py collectstatic --noinput
      && python manage.py runserver 0.0.0.0:8000"
    volumes:
      - media-cdn-data:/app/media_cdn
    deploy:
      restart_policy:
        condition: on-failure

    tty: true

  database:
    image: "postgres:12" # use latest official postgres version
    env_file:
      - database.env # configure postgres
    volumes:
      - database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
volumes:
  database-data: # named volumes can be managed easier using docker-compose
  media-cdn-data:

This is my nginx file:这是我的 nginx 文件:

server {
       server_name app.platformname.com;
             location /media/ {
                 proxy_pass http://127.0.0.1:8400/;
                 alias /app/media_cdn/;
             }
       location / {
            proxy_pass http://127.0.0.1:8400/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_read_timeout 3600;
            proxy_set_header Connection '';
            # these two lines here
            proxy_http_version 1.1;
        }
}

Any help is appreciated,I have been struggling with it for the past 4 days:)感谢您的帮助,过去 4 天我一直在努力解决这个问题:)

Also i don't see you have any nginx block in your docker-compose file.我也没有看到你的 docker-compose 文件中有任何nginx块。 Your你的

You have try this way both in your backend section and reverse proxy section您在后端部分和反向代理部分都尝试过这种方式

 - "./dist/static_cdn:/backend/static_cdn" # map physical machine

and in the nginx file add this:在 nginx 文件中添加:

 alias /backend/static_cdn;

Note: Please update your docker-compose file for nginx/reverse proxy similar to this注意:请为类似于此的 nginx/反向代理更新您的docker-compose文件

Why do you have to map with local physical machine instead of the container?为什么你必须用本地物理机而不是容器来 map? coz, you will get permission denied error if you don't map with local machine to container因为,如果你不使用本地机器到容器的 map,你将得到权限被拒绝的错误

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

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