[英]Unable to serve django static files via nginx and docker
我在使用 docker 和 django 配置 nginx 服务器时遇到问题。 我认为问题在于音量路径。
这是我的目录结构
-nginx
--default.conf
--Dockerfile
-portfolio_app (django webpapp)
--main_app
---settings.py
---wsgi.py
--sub_app
---views.py
---static
---media
-docker-compose.yml
-Dockerfile (django related)
-entrypoint.sh (to start django server)
关于 django 它工作,但我无法提供静态文件。 我认为我没有正确给出路径。
这是与 django 相关的 Dockerfile
FROM python:3.8.13-slim-buster
WORKDIR /app
RUN pip install --upgrade pip
COPY ./requirements.txt ./
RUN pip install -r requirements.txt
COPY ./portfolio_app ./
COPY ./entrypoint.sh ./
ENTRYPOINT ["sh","/app/entrypoint.sh"]
nginx 文件
默认.conf
upstream django {
server portfolio_application:8000;
}
server {
listen 80;
location / {
proxy_pass http://django;
}
location /static/ {
alias /static_volume/;
}
}
Dockerfile
FROM nginx:1.19.0-alpine
COPY ./default.conf /etc/nginx/conf.d/default.conf
这是 docker-compose.yml 文件
version: '3.3'
services:
portfolio_application:
build:
context: .
container_name: portfolio_app
volumes:
- static_volume:/app/sub_app/static
- media_volume:/app/sub_app/media
ports:
- "8000:8000"
env_file:
- .env
nginx:
build: ./nginx
volumes:
- static_volume:/app/sub_app/static
- media_volume:/app/sub_app/media
ports:
- "80:80"
depends_on:
- portfolio_application
volumes:
media_volume:
static_volume:
我不确定 yml 文件中卷的路径和 default.conf 的设置。 这是日志
Attaching to portfolio_app, docker_nginx_1
portfolio_app | [2022-07-21 09:22:10 +0000] [7] [INFO] Starting gunicorn 20.1.0
nginx_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
portfolio_app | [2022-07-21 09:22:10 +0000] [7] [INFO] Listening at: http://0.0.0.0:8000 (7)
portfolio_app | [2022-07-21 09:22:10 +0000] [7] [INFO] Using worker: sync
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
portfolio_app | [2022-07-21 09:22:10 +0000] [9] [INFO] Booting worker with pid: 9
nginx_1 | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx_1 | 10-listen-on-ipv6-by-default.sh: /etc/nginx/conf.d/default.conf differs from the packages version, exiting
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_1 | 172.19.0.1 - - [21/Jul/2022:09:22:13 +0000] "GET / HTTP/1.1" 200 48080 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0" "-"
nginx_1 | 2022/07/21 09:22:13 [error] 29#29: *1 open() "/static_volume/lib/animate/animate.min.css" failed (2: No such file or directory), client: 172.19.0.1, server: , request: "GET /static/lib/animate/animate.min.css HTTP/1.1", host: "0.0.0.0", referrer: "http://0.0.0.0/"
nginx_1 | 172.19.0.1 - - [21/Jul/2022:09:22:13 +0000] "GET /static/lib/animate/animate.min.css HTTP/1.1" 404 153 "http://0.0.0.0/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0" "-"
编辑这就是我在settings.py
中添加静态url和文件的方式
MEDIA_ROOT = os.path.join(BASE_DIR, 'sub_app','media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'sub_app','static')
在您的 nginx 服务中,您已安装卷,如下所示。 其中 static_volume 是主机卷, /app/sub_app/static
是挂载到的容器目录。
static_volume:/app/sub_app/static
但是在您的 nginx 配置文件中,您将静态请求路由到/static_volume/
。 相反,您需要将其指向您的容器目录,如下所示
location /static/ {
alias /app/sub_app/static/;
}
默认配置:
而不是这个:
location /static/ {
alias sub_app/static/;
}
尝试这个:
location /static/ {
alias ./static/;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.