django - 静态文件不提供,但可以直接访问。 Django, docker ,nginx

标签 django docker nginx

我正在尝试使用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;添加到httpnginx.conf部分中来解决此问题

关于django - 静态文件不提供,但可以直接访问。 Django, docker ,nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63623880/

相关文章:

python - 在 Django 模型中保存时区

python - 自定义 Django 模型字段的字段查找功能

java - 使用 eirslett :frontend-maven-plugin 在 gitlab.com 上运行 maven build

docker - Artifactory 不会拉动 Windows 图像的某些层

python - 使用 unicode_literals 在 Flask 应用程序中添加 header

Django URL - 如何通过干净的 URL 传递项目列表?

django - 通过整数字段在 Elasticsearch_dsl 中进行范围查询

docker - 如何使用Docker容器运行Kong API Gateway?

python - nginx 拦截 google oauth 重定向

php - 我无法在 nginx 中测试 PHP