我尝试使用nginx和gunicorn加载静态文件,当我转到http://0.0.0.0/static/css/base.css
static hosted via nginx时,它可以工作,但当我添加django端口http://0.0.0.0:8000/static/css/base.css
static not found using django server时,它不工作
这些是我在nginx容器中得到的错误
nginx_1 | 172.22.0.1 - - [04/Oct/2023:04:59:33 +0000] "GET /static/css/base.css HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36" "-"
nginx_1 | 2023/10/04 04:59:34 [error] 8#8: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.22.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost", referrer: "http://localhost/static/css/base.css"
nginx_1 | 172.22.0.1 - - [04/Oct/2023:04:59:34 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://localhost/static/css/base.css" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36" "-"
这就是我所尝试的,在conf.d中我把容器名称作为server_name,并试图添加类型来确定css文件,但它不起作用settings.py
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
Dockerfile
FROM python:3.10
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONBUFFERED=1 \
POETRY_VERSION=1.4.2 \
POETRY_VIRTUALENVS_CREATE="false"
RUN pip install "poetry==$POETRY_VERSION"
WORKDIR /education_platform
COPY pyproject.toml poetry.lock docker-entrypoint.sh ./
RUN poetry install --no-interaction --no-ansi --no-dev
COPY . /education_platform
EXPOSE 8000
RUN chmod +x wait-for-it.sh
docker-compose.yaml
version: '3'
services:
db:
image: postgres:13.1-alpine
restart: unless-stopped
env_file:
- ./.env
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
cache:
image: redis:7.0.4
restart: unless-stopped
volumes:
- .data/cache:/data
web:
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
command: [ "/education_platform/wait-for-it.sh", "db:5432", "--",
"gunicorn", "education_platform.wsgi:application", "-b", "0.0.0.0:8000"]
env_file:
- ./.env
volumes:
- .:/education_platform
- ./static:/education_platform/static
ports:
- "8000:8000"
environment:
- DJANGO_SETTINGS_MODULE=education_platform.settings.prod
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
- cache
nginx:
image: nginx:1.23.1
restart: unless-stopped
volumes:
- ./config/nginx/conf.d:/etc/config/nginx/conf.d/default.conf
- ./static:/usr/share/nginx/html/static
ports:
- "80:80"
command: ["/bin/sh", "-c", "nginx -g 'daemon off;'"]
depends_on:
- web
conf.D
server {
listen 80;
server_name education_platform_web_1;
error_log stderr warn;
access_log /dev/stdout main;
location / {
proxy_pass http://education_platform_web_1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
}
location /static/ {
alias /usr/share/nginx/html/static/;
}
location /media/ {
alias /education_platform/media/;
}
types {
text/javascript js;
application/javascript js;
text/css css;
}
}
1条答案
按热度按时间ycggw6v21#
据我所知,有几件事不见了:
为了防止在你的docker-compose文件中出现大量的命令,我建议在你的Dockerfile中遵循以下命令:
从这个(docker-compose):
类似这样的东西(在你的Dockerfile的末尾):
现在你可能想知道,我的
wait-for-it.sh
脚本到哪里去了?entrypoint.sh
在做什么下面是
entrypoint.sh
文件:还有一件事,我看到了两种指定环境变量的方法。env_file和environment条目都在docker-compose文件中使用。现在,在你的情况下可能有一个有效的理由,但如果没有,最好使用一个或另一个: