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

Docker+Django+Gunicorn环境下Nginx无法正常工作求助

Django容器化Nginx代理故障排查求助

已花费一周多时间将现有Django应用容器化,复制生产环境配置后卡在Nginx相关环节。目前Django应用、SQL数据库、Redis实例及CRON服务均能正常运行,但Nginx容器出现问题:Nginx无法在localhost的80端口提供服务,但仍可通过8000端口直接访问应用。所有容器均显示运行正常,已进入容器确认文件存在且配置无误,恳请提供排查思路。


docker-compose.yml

version: '3.8'

services:
  xyz_web:
    image: xyz-web:latest
    networks:
      - xyz_backend
    volumes:
      - ../app:/app
      - xyz_static:/app/static
    depends_on:
      - xyz_db
      - xyz_cache
    # ports:
    #   - 8000:8000
    command: gunicorn xyz_src.wsgi:application --bind :8000 --workers 1 --threads=3 --access-logfile -
    env_file:
      - django/.env

  xyz_cron:
    image: xyz-web:latest
    networks:
      - xyz_backend
    volumes:
      - ../app:/app
    depends_on:
      - xyz_db
      - xyz_cache
    command: cron -f
    env_file:
      - django/.env
  
  xyz_cache:
    image: xyz-cache:latest
    networks:
      - xyz_backend
    volumes:
      - ../build/redis/data:/data
    command: /bin/sh -c "redis-server --requirepass $$REDIS_CACHE_PASS"
    env_file:
      - redis/.env

  xyz_db:
    image: xyz-db:latest
    networks:
      - xyz_backend
    volumes:
      - ../build/mysql/data:/var/lib/mysql
      - ../build/mysql/mysqld:/var/run/mysqld
    ports:
      - 3306:3306
    env_file:
      - mysql/.env
      
  xyz_proxy:
    image: xyz-proxy:latest
    networks:
      - xyz_backend
    volumes:
      - xyz_static:/app/static
    ports:
      - 8000:80
    depends_on:
      - xyz_web

networks:
  xyz_backend:
    external: false
    driver: bridge

volumes:
  xyz_static:

Django应用Dockerfile

FROM python:3.10.13-bullseye

ENV PYTHONUNBUFFERED 1

# update and upgrade the image
RUN set -x \
    && apt-get update \
    && apt-get upgrade -y \
    && apt-get install cron -y

# install TA-Lib dependency
RUN set -x \
    && wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz \
    && tar -xzf ./ta-lib-0.4.0-src.tar.gz \
    && cd ta-lib \
    && ./configure \
    && make \
    && make install

# upgrade the pip package manager
RUN pip install --upgrade pip

WORKDIR /app

ADD app /app

COPY build/django/requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

# EXPOSE 8000

SQL数据库Dockerfile

FROM mysql:8.0.34-debian

# update and upgrade the image
RUN set -x \
    && apt-get update \
    && apt-get upgrade -y

CMD ["--character-set-server=utf8mb4", "--collation-server=utf8mb4_general_ci", "--skip-character-set-client-handshake"]

WORKDIR /app

Redis实例Dockerfile

FROM redis:6.2.13-bookworm

# update and upgrade the image
RUN set -x \
    && apt-get update \
    && apt-get upgrade -y

COPY build/redis/etc/redis /etc/redis

Nginx代理Dockerfile

FROM nginx:1.25.2-bookworm

RUN set -x \
    && apt-get update \
    && apt-get upgrade -y

RUN rm /etc/nginx/conf.d/default.conf
COPY build/nginx/etc/nginx/conf.d/prod.conf /etc/nginx/conf.d

EXPOSE 80

Nginx prod.conf配置

upstream prod_server {
    server xyz_web:8000;
}

server {

    listen 80;
    server_name localhost;

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

    location /static/ {
        autoindex on;
        root /app/staticfiles;
    }
}

容器运行状态

$ docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED        STATUS        PORTS                               NAMES
c0acefca3fdd   xyz-proxy:latest   "/docker-entrypoint.…"   32 hours ago   Up 32 hours   0.0.0.0:8000->80/tcp                build-xyz_proxy-1
ee40391a2259   xyz-web:latest     "gunicorn xyz_sr…"       32 hours ago   Up 32 hours                                       build-xyz_web-1
5b47db517201   xyz-web:latest     "cron -f"                32 hours ago   Up 32 hours                                       build-xyz_cron-1
d93d5e1c568b   xyz-db:latest      "docker-entrypoint.s…"   32 hours ago   Up 32 hours   0.0.0.0:3306->3306/tcp, 33060/tcp   build-xyz_db-1
b944a33ea68a   xyz-cache:latest   "docker-entrypoint.s…"   32 hours ago   Up 32 hours   6379/tcp                            build-xyz_cache-1

内容的提问来源于stack exchange,提问作者Victor Velchev

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 04:11:08