[英]Nginx serve static files for a Django app
我有一個要部署的 Django 應用程序。 在這個階段,我似乎無法從我的 Nginx 容器中提供我的靜態文件。
我的項目的設置類似於此。
我已將我的圖像放入{% 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 為我的靜態文件提供服務上。
您需要將文件夾/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: web
但是web
容器沒有安裝任何卷,因為你需要添加volumes: - /usr/src/app/static
使用 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。
如果你用 apache 替換 nginx,那么就相互通信而言,沒有什么需要改變的,postgresql 與 percona 或其他什么都一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.