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

Docker部署Django+Gunicorn后,Nginx无法转发至应用仅显示默认页面的问题排查求助

Docker部署Django+Gunicorn后,Nginx无法转发至应用仅显示默认页面的问题排查求助

大家好,我最近在折腾Docker部署Django项目,目前已经完成了以下操作:

  • Django测试页面在本地localhost运行完全正常
  • 成功把项目和Docker关联起来
  • 也配置好了Docker与Gunicorn的连接

但现在碰到了一个棘手的问题:当我尝试通过Nginx访问应用时,页面只显示Nginx的默认欢迎页,根本没有转发到我创建的Django应用上,试了好多次都没解决,麻烦各位帮忙看看哪里出问题了,非常感谢!

下面是我用到的所有配置文件:

nginx/Dockerfile

FROM nginx:latest
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

nginx/nginx.conf

upstream daniel_blog {
    server web:8000;
}

server {
    listen 80;

    server_name localhost;

    # Default location
    location / {
        proxy_pass http://daniel_blog;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    # Static files
    location /static/ {
        alias /usr/src/app/_static/;
    }

    # Media files
    location /media/ {
        alias /usr/src/app/_media/;
    }
}

.env.dev

DEBUG="1"
SECRET_KEY="django-insecure-88ndzs0z0uzko4x5f=*ngu=#h+a(k=!#ooyvt*0h+78r1n=^oy"
DJANGO_ALLOWED_HOSTS = localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=daniel_blog_dev
SQL_USER=daniel_blog_user
SQL_PASSWORD=daniel_blog_db_password
SQL_HOST=db
SQL_PORT=5432

.env.prod

DEBUG=0
SECRET_KEY=w4+=--yyyigm1-=na@9et*1ldt-q-x4x=57m2xkiru6!oxeh5b
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=daniel_blog_prod
SQL_USER=daniel_blog_user
SQL_PASSWORD=daniel_blog_db_password
SQL_HOST=db
SQL_PORT=5432

.env.prod.db

POSTGRES_USER=daniel_blog_user
POSTGRES_PASSWORD=daniel_blog_db_password
POSTGRES_DB=daniel_blog_prod

docker-compose.yml

services:
  nginx:
    build: ./nginx
    volumes:
      - static_volumes:/usr/src/app/_static
      - media_volumes:/usr/src/app/_media
    ports:
      - 80:80
    depends_on:
      - web
  web:
    build: .
    command: gunicorn daniel_blog.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volumes:/usr/src/app/_static
      - media_volumes:/usr/src/app/_media
      - ./:/usr/src/app/_media
    expose:
      - 8000
    env_file:
      - ./.env.prod
    depends_on:
      - db
  db:
    image: postgres:17.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db
volumes:
  postgres_data:
  static_volumes:
  media_volumes:

Dockerfile

FROM python:3.11-slim

RUN apt-get update && apt-get install -y \
    build-essential \
    libffi-dev \
    libssl-dev \
    && rm -rf /var/lib/apt/lists/*

RUN apt-get update && apt-get install -y libpq-dev gcc
    
WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY requirements.txt ./

COPY . .

settings.py中的DATABASES配置

DATABASES = {
    'default': {
        'ENGINE': os.environ.get("SQL_ENGINE", 'django.db.backends.postgresql'),
        'NAME': os.environ.get('SQL_DATABASE', 'daniel_blog_dev'),
        'USER': os.environ.get('SQL_USER', 'daniel_blog_user'),
        'PASSWORD': os.environ.get('SQL_PASSWORD', 'daniel_blog_db_password'),
        'HOST': os.environ.get('SQL_HOST', 'localhost'),
        'PORT': os.environ.get('SQL_PORT', '5432'),
        "OPTIONS": {
            "client_encoding": "UTF8"
        }
    }
}

备注:内容来源于stack exchange,提问作者Daniel Choi斓滊嫟雼堨棙

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 17:03:14