NGINX为Django App提供静态文件

我有一个Django应用程序,我试图部署。

在这个阶段,我似乎无法从我的nginx容器提供我的静态文件。

我的项目就像这里一样

我把我的图片放到{% static "minimal/theme/assets/img/pic.jpg"%}目录下。

我的networking应用程序的文件结构是:

 . ├── 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容器中,我有一个configuration文件在启用网站的文件夹中,如下所示:

 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服务我的静态文件。

有人可以帮忙吗? 如果你需要额外的信息或代码片段,请让我知道,因为我真的想学习如何解决这个问题。

您需要将您的文件夹/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 

在你的nginxconfiguration中,你告诉nginx从这个目录/usr/src/app/static提供静态文件,但是nginx容器除非你链接它,否则没有它。 你几乎这样做,你有volumes_from: web但是web容器没有安装任何卷,因为你需要添加volumes: - /usr/src/app/static

使用compose v1语法没有意义,通常使用数据卷容器(下面的appcode)来完成。 这可以确保您基本上可以根据数据卷容器上的需要devise文件夹结构,然后将其安装在所有您需要的容器中。

这些是你需要做的改变(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。

如果你用apachereplacenginx,没有什么需要相互通信的变化,postgresql vs percona也一样。