Nginx不支持Docker、Django和Gunicorn

68de4m5k  于 7个月前  发布在  Nginx
关注(0)|答案(1)|浏览(83)

我一直在努力Dockerize一个现有的Django应用程序一个多星期了,我有点不知道如何继续下去。我已经复制了prod环境和环境设置,但我仍然在努力与最后一部分,是与Nginx有关。我将概述我的Docker文件和YML文件,使应用程序的生活。Django应用程序,SQL DB,Redis示例和CRON服务器都在工作,但最后一块拼图是Nginx容器。
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 DB的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


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;
    }
}


问题是Nginx不通过localhost在端口80上提供流量,但我仍然可以在端口8000上使用该应用程序。我不确定我错过了什么,任何关于错误的提示都将非常感谢。我还设置了Nginx来提供静态流量,但我不知道它是否工作,因为代理似乎没有运行,即使所有容器都报告工作。我已经执行了艾德进入他们,以确认文件在那里,他们似乎看起来很好:

$ 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

oug3syen

oug3syen1#

你提到nginx在端口8000而不是80上为应用提供服务是正确的。根据你在docker-compose上的nginx配置,

ports:
  - 8000:80

字符串
左侧表示host machine上的端口,右侧是container内部的端口。因此,您的应用程序不在端口80上提供服务,它在端口8000上提供服务。要在主机上的端口80上提供服务,请将8000也更改为80。还要确保端口80在主机上可用,并且您没有服务使用它。如果它不可用,您将不得不关闭这些服务。
nginx的portsMap应该是:

ports:
    - 80:80

相关问题