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

Docker+Nginx部署Django应用时静态文件无法加载的配置求助

Docker+Nginx部署Django应用时静态文件无法加载的配置求助

大家好,我目前正在尝试把自己的Django应用部署到虚拟机的Docker容器中,用Nginx作为反向代理服务器,但现在遇到了静态文件无法加载的问题——Admin页面加载出来完全没有样式,就是缺少静态文件的状态。我不太清楚该怎么调整Nginx的配置来确保静态文件能被正确服务,下面是我的所有配置文件,麻烦各位帮忙排查一下问题所在!

Dockerfile

FROM python:3.10

ENV PYTHONUNBUFFERED=1

RUN mkdir -p /kings

WORKDIR  /kings

COPY requirements.txt /kings/requirements.txt

COPY ./kings /kings

RUN pip install -r /kings/requirements.txt

COPY ./entrypoint.sh /kings/entrypoint.sh

CMD ["sh", "/kings/entrypoint.sh"]

docker-compose.yml

version: '3'

services:
  kings-app:
    build:
      context: .
      dockerfile: ./DockerFile
    volumes:
      - .:/kings
    ports:
      - "8000:8000"
    image: kings-app:kings
    container_name: kings-ap
    depends_on:
      - kings-redis
    environment:
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
      - DB_HOST=${DB_HOST}
      - DB_PORT=${DB_PORT}
      - ALLOWED_HOSTS=${ALLOWED_HOSTS}
      - REDIS_HOST=${REDIS_HOST}
      - REDIS_PORT=${REDIS_PORT}

  kings-redis:
    image: redis:alpine
    container_name: kings-redis

  nginx:
    image: nginx:alpine
    container_name: kings-nginx
    ports:
      - "1337:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/nginx.conf
      - ./static:/kings/static
    depends_on:
      - kings-app

nginx.conf

worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;

    sendfile on;

    upstream app_server {
        server kings-app:8000;
    }

    server {
        listen 80;
        server_name localhost;

        location /static/ {
            alias /kings/static/;
        }

        location / {
            proxy_pass http://app_server;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}

entrypoint.sh

#!/bin/sh

python manage.py migrate --no-input
python manage.py collectstatic --no-input
gunicorn kings.wsgi:application --bind 0.0.0.0:8000

静态文件相关设置说明

我的静态文件相关配置都放在项目的根目录下。

备注:内容来源于stack exchange,提问作者B.obed

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 11:17:51