【问题标题】:docker, nginx, django and how to serve static filesdocker、nginx、django 以及如何提供静态文件
【发布时间】:2014-10-05 23:07:30
【问题描述】:

目标:用于生产 django 网站部署的 docker 容器集。

我在这个过程中的挂断是通常 nginx 直接为静态文件提供服务...根据我对使用 docker 的良好架构的理解,您将有一个用于 wsgi 服务器的容器(可能是 gunicorn),一个单独的 nginx 容器上游服务器配置指向您的 gunicorn 容器。 nginx容器可以在多个gunicorn容器之间进行负载均衡。

但这意味着我必须在 nginx 容器中安装我的 django 应用程序的静态文件,这似乎是一种不好的做法,因为它的主要目标实际上是负载平衡

拥有三个容器是否更好:nginx、gunicorn 和一个用于静态文件的专用静态服务器(可能是 nginx 或 lighthttpd)?

【问题讨论】:

    标签: django nginx docker


    【解决方案1】:

    关于提供静态文件,您的选项取决于您的应用程序的功能。有一个非常漂亮的工具叫做dj-static,它可以通过添加非常少的代码来帮助您提供静态文件。

    文档相当简单,您只需关注these steps.

    【讨论】:

    • 这会导致我的设置出现错误的网关(在调整 django 应用程序以与 dj-static 一起使用之前,该网关正在工作)
    • 我需要一些东西来在我的 gunicorn docker 上提供静态文件。 dj-static 完美地做到了。
    【解决方案2】:

    我从Michael Hampton 找到了这个答案: “这仅在进程位于同一主机、VM 或容器中时才有效,因为它会尝试与同一台机器建立连接。当它们位于不同的容器中时,它不起作用。

    您需要更改您的 nginx 配置,以便它使用 uwsgi 容器的内部 IP 地址。" Link from the post

    如果您将 Nginx 放在不同的容器中,您必须牢记这一点,您还必须设置 nginx.conf,将您的静态文件目录作为别名,以防止安全问题。

    我希望这段代码对每个人都有效,我花了几个人的时间才弄清楚如何编写 Gunicorn、docker 和 Nginx:

    # nginx.conf
      upstream djangoA {
           server $DOCKER_CONTAINER_SERVICE:9000 max_fails=3 fail_timeout=0;
           # In my case looks like: web:9000
      }
    Server {
        include mime.types;
        # The port your site will be served on      
        listen 80;
        # the domain name it will serve for
        server_name $YOUR_SERVER_NAME;# substitute your machine's IP address or FQDN
        charset utf-8;
        #Max upload size
        client_max_body_size 512M;   # adjust to taste
        location /site_media {
           alias $DIRECTORY_STATIC_FILES/site_media;#your Django project's media   files have to be inside of the container have nginxs, you can copy them with volumes.
        expires 30d;
        }
    
        location / {
          try_files $uri @proxy_to_app;
        }
    
       # Finally, send all non-media requests to the Django server.
       location @proxy_to_app {
         proxy_set_header X-Real-IP $remote_addr;
         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_pass http://djangoA;
       }
    }
    

    对于 docker-compose:

    #production.yml
    version: '2'
    
    services:
      db:
        extends:
          file: base.yml
          service: db
    
      nginx:
        image: nginx:latest
      volumes:
          - ./nginx:/etc/nginx/conf.d/
          - ./$STATIC_FILE_ROOT/site_media:/$STATIC_FILE_ROOT/site_media
      ports:
          - "80:80"
        depends_on:
          - web
    
    
      web:
        extends:
          file: base.yml
          service: web
        build:
          args:
            - DJANGO_ENV=production
        command: bash -c "python manage.py collectstatic --noinput && chmod 775 -R project/site_media/static && gunicorn project.wsgi:application"
        volumes:
          - ./$DIRECTORY_APP:/$DIRECTORY_APP
        ports:
          - "9000:9000"
        depends_on:
          - db
    
    volumes:
      db_data:
        external: true
    

    【讨论】:

      【解决方案3】:

      如果是使用 docker 和/或 kubernetes 的 django 应用,请查看 whitenoise http://whitenoise.evans.io/en/stable/ 以在此处提供解决方案。

      这是一个直截了当的建议,但我花了很多时间搜索才发现它被引用。

      【讨论】:

        猜你喜欢
        • 2019-05-31
        • 2019-02-22
        • 2020-06-17
        • 2023-03-05
        • 2017-11-19
        • 2021-12-02
        • 2020-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多