我正在尝试使用postgres,gunicorn和nginx将django项目进行 jetty 化。我的问题是不提供静态文件。但是,在浏览器控制台中,我看到所有静态文件都已正确加载(代码200)。我也可以轻松地直接访问任何静态文件,即127.0.0.1:8080/static/admin/.../base.css
。 Nginx在控制台中也不显示任何错误。是什么让我更加奇怪,我的redoc.yaml
已正确加载。
这是我的 settings.py静态的一部分:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
这是我的主要Dockerfile的:FROM python:latest
RUN mkdir /code
COPY requirements.txt /code
RUN pip install -r /code/requirements.txt
COPY . /code
WORKDIR /code
CMD gunicorn api_yamdb.wsgi:application --bind 0.0.0.0:8000
这是nginx 的 Dockerfile:FROM nginx:latest
COPY nginx/nginx.conf /etc/nginx/nginx.conf
这是 nginx.conf 文件:events {}
http {
server {
location /static/ {
root /etc/nginx/html/;
}
location / {
proxy_pass http://web:8000;
}
}
}
最后是我的 docker-compose :version: '3.8'
volumes:
postgres_data:
static:
services:
db:
image: postgres:latest
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env
web:
build: .
restart: always
command: gunicorn api_yamdb.wsgi:application --bind 0.0.0.0:8000
volumes:
- static:/static
ports:
- "8000:8000"
depends_on:
- db
env_file:
- ./.env
nginx:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./static:/etc/nginx/html/static
欢迎任何帮助。谢谢。
最佳答案
可以通过将include mime.types;
添加到http
的nginx.conf
部分中来解决此问题
关于django - 静态文件不提供,但可以直接访问。 Django, docker ,nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63623880/