You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx处理Django静态文件提示“文件或目录不存在”问题排查

问题:Nginx无法加载Django静态文件

错误日志

[error] 20#20: *1 open() "/home/app/web/staticfiles/admin/css/nav_sidebar.css" failed (2: No such file or directory), client: 172.20.0.1, server: , request: "GET /staticfiles/admin/css/nav_sidebar.css HTTP/1.1", host: "127.0.0.1:1337", referrer: "http://127.0.0.1:1337/admin/"

确认请求路径正确,但Nginx无法从指定目录返回响应,请问哪里配置出错了?以下是相关配置文件:

nginx.conf

upstream hello_django {
    server web:8000;
}
server {
    listen 80;
    location / {
        proxy_pass http://hello_django;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

     location /staticfiles/ {
        alias /home/app/web/staticfiles/;
    }
}

docker-compose.yml

version: '3.9'
services:
  web:
    build: .
    command: gunicorn pets.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/web/staticfiles
    expose:
      - 8000
    env_file:
      - .env
    depends_on:
      - db
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - .env
  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/app/web/staticfiles
    ports:
      - 1337:80
    depends_on:
      - web
volumes:
  postgres_data:
  static_volume:

Dockerfile

FROM python:3.10.0-alpine
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
RUN apk update
RUN apk add postgresql-dev gcc python3-dev musl-dev
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./entrypoint.sh .
RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh
COPY . .
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $HOME
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/staticfiles
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]

entrypoint.sh

#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $POSTGRES_HOST $POSTGRES_PORT; do
      sleep 0.1
    done

    echo "PostgreSQL started"
fi

cd pets

exec "$@"

解决方案

核心问题是没执行Django的collectstatic命令,静态文件根本没被收集到共享卷static_volume中,所以Nginx找不到文件。

修复步骤:

  1. 在entrypoint.sh中添加静态文件收集命令
    修改entrypoint.sh,在cd pets之后、exec "$@"之前加入收集命令:

    python manage.py collectstatic --noinput
    

    修改后的完整脚本:

    #!/bin/sh
    
    if [ "$DATABASE" = "postgres" ]
    then
        echo "Waiting for postgres..."
    
        while ! nc -z $POSTGRES_HOST $POSTGRES_PORT; do
          sleep 0.1
        done
    
        echo "PostgreSQL started"
    fi
    
    cd pets
    python manage.py collectstatic --noinput
    exec "$@"
    
  2. 确认Django静态文件配置
    检查项目settings.py中是否有以下配置:

    STATIC_URL = '/staticfiles/'
    STATIC_ROOT = '/home/app/web/staticfiles/'
    
  3. 重构并重启容器
    执行命令:

    docker-compose down
    docker-compose up --build
    

额外检查点:

  • 确认static_volume卷的权限:容器内/home/app/web/staticfiles目录的所有者需与运行Gunicorn的用户一致(默认是root,若用非root用户需调整权限)。
  • Nginx的location /staticfiles/配置无问题,alias路径与共享卷挂载路径匹配,末尾斜杠也正确。

内容的提问来源于stack exchange,提问作者Anonymous Wizard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 08:59:20