gpt4 book ai didi

django - 当主机系统上只有Nginx时,使用Docker的静态文件

转载 作者:行者123 更新时间:2023-12-02 21:10:57 25 4
gpt4 key购买 nike

我的主机(Ubuntu服务器)上有Nginx。使用Docker时应如何设置静态文件?

version: '2'

services:
postgres:
image: postgres:9.6
redis:
image: "redis:alpine"
web: &django
restart: always
environment:
- DJANGO_SECRET_KEY=local
image: web
build:
context: .
dockerfile: ./compose/production/web/Dockerfile
command: /gunicorn.sh
depends_on:
- postgres
- redis
links:
- redis
ports:
- "8083:5000"

在另一个项目(没有Docker)中,我在 sites-enabled/mysite中具有此设置:
location /static/ {
root /home/myproject;
}

最佳答案

在这里,您可以使用volume从容器到主机系统共享静态文件夹,如下所示:

version: '2'

services:
postgres:
image: postgres:9.6
redis:
image: "redis:alpine"
web: &django
restart: always
environment:
- DJANGO_SECRET_KEY=local
image: web
build:
context: .
dockerfile: ./compose/production/web/Dockerfile
volumes:
- /path/to/static/folder:/static
command: /gunicorn.sh
depends_on:
- postgres
- redis
links:
- redis
ports:
- "8083:5000"

现在,使用nginx从该路径提供这些内容:
location /static/ {    
autoindex on;
alias /path/to/static/folder/;
}

在这里,我假设您正在使用 /static作为docker中django项目的静态根。

关于django - 当主机系统上只有Nginx时,使用Docker的静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54692093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com