【问题标题】:Nginx serve static files for a Django appNginx 为 Django 应用程序提供静态文件
【发布时间】:2016-12-23 02:26:25
【问题描述】:

我有一个要部署的 Django 应用程序。在这个阶段,我似乎无法从我的 Nginx 容器中提供我的静态文件。

我的项目设置为here

我已将图片放入{% static "minimal/theme/assets/img/pic.jpg"%} 目录。

我的网络应用的文件结构是:

.
├── Dockerfile
├── docker_django
│   ├── __init__.py
│   ├── apps
│   │   ├── __init__.py
│   │   └── todo
│   │       ├── __init__.py
│   │       ├── admin.py
│   │       ├── fixtures
│   │       │   ├── foodprice.json
│   │       │   ├── location.json
│   │       │   └── menuitem.json
│   │       ├── forms.py
│   │       ├── models.py
│   │       ├── templates
│   │       │   ├── _base-original.html
│   │       │   ├── feedback_template.txt
│   │       │   ├── home-original.html
│   │       │   └── todo
│   │       │       ├── base-original.html
│   │       │       ├── base-template.html
│   │       │       ├── base.html
│   │       │       ├── detail.html
│   │       │       ├── email.html
│   │       │       ├── feedback.html
│   │       │       ├── home.html
│   │       │       ├── home_old.html
│   │       │       ├── location.html
│   │       │       ├── menu.html
│   │       │       ├── results.html
│   │       │       ├── test.html
│   │       │       └── thanks.html
│   │       ├── tests.py
│   │       ├── urls.py
│   │       └── views.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── requirements.txt
└── static
    ├── main.css
    └── minimal
        └── theme
            ├── assets
            │   ├── css
            │   ├── img
            │   │   ├── pic1.jpg
            └── index.html

在我的 Nginx 容器中,启用站点的文件夹中有一个配置文件,如下所示:

server {

listen 80;
server_name localhost;
charset utf-8;

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

location / {
    proxy_pass http://web:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

我的settings.py相关部分的片段如下:

BASE_DIR =     os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print "base dir path", BASE_DIR
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
....
STATIC_URL = '/static/'
# STATICFILES_DIRS = (
#     os.path.join(BASE_DIR, 'static'),
# )
# print(STATICFILES_DIRS)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

production.yml 就像:

web:
  restart: always
  build: ./web
  expose:
    - "8000"
  links:
    - postgres:postgres
    - redis:redis
  env_file: .env
  command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000

nginx:
  restart: always
  build: ./nginx/
  ports:
    - "80:80"
  volumes:
    - /www/static
  volumes_from:
    - web
  links:
    - web:web

postgres:
  restart: always
  image: postgres:latest
  ports:
    - "5432"
  volumes:
    - pgdata:/var/lib/postgresql/data/

redis:
  restart: always
  image: redis:latest
  ports:
    - "6379"
  volumes:
    - redisdata:/data

当我在我的机器上本地运行时,静态文件夹中的所有 JavaScript 和图像都会加载并完美运行。当我部署到数字海洋时,静态文件没有被提供,我被困在如何让 Nginx 为我的静态文件提供服务。

【问题讨论】:

    标签: django web-services nginx deployment docker


    【解决方案1】:

    您需要将您的文件夹 /usr/src/app/static 挂载为您的 Web 容器中的卷。这样,ngnix 将能够访问它。

    web:
      restart: always
      build: ./web
      expose:
        - "8000"
      volumes:
        - /usr/src/app/static
      links:
        - postgres:postgres
        - redis:redis
      env_file: .env
      command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000
    

    在您的nginx 配置中,您告诉 nginx 从该目录/usr/src/app/static 提供静态文件,但nginx 容器没有它,除非您链接它。你几乎做到了,你有volumes_from: webweb 容器没有挂载任何卷,因为你需要添加volumes: - /usr/src/app/static

    【讨论】:

      【解决方案2】:

      使用 compose v1 语法是没有意义的,这通常使用 data-volume-containers(下面的应用程序代码)来完成。这确保您基本上可以根据需要在数据卷容器上设计文件夹结构,然后将其安装在您需要的所有容器中。

      这些是您需要做的更改(docker composer v2 语法)

      version: '2'
        services:
          nginx:
            volumes_from:
              - appcode:ro
      
          web:
            volumes_from:
              - appcode:rw
      
          appcode:
            image: cogniteev/echo
            container_name: dwstorage
            command: echo 'Data Volume Container'
            volumes:
               - appcode:/www/static
      volumes:
        appcode:
          driver: local 
      

      (未询问,但在这种情况下可能有用) 在这一点上的一个提示,不要像守护进程那样命名你的服务,而是给它们语义名称。所以nginx可以变成httpd,web变成app,postgres变成db。

      如果将 nginx 替换为 apache,则无需在相互通信方面进行任何更改,对于 postgresql 与 percona 或其他任何东西都是如此。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-07
        • 2016-01-28
        • 2021-12-02
        • 2020-09-14
        • 1970-01-01
        • 1970-01-01
        • 2020-03-06
        • 2013-06-27
        相关资源
        最近更新 更多