首页 > 解决方案 > django+uwsgi+docker 同时连接到上游

问题描述

我通过 docker 创建了基础设施。

为了部署 django,使用了 uwsgi 和 nginx。

这是我的配置文件。

[码头工人-compose.yml]

version: '3.7'

services:
  nginx:
    build:
      context: .
      dockerfile: docker/nginx/Dockerfile
    container_name: nginx
    hostname: nginx
    ports:
      - '80:80'
    networks:
      - backend
    restart: on-failure
    links:
      - web_service
    depends_on:
      - web_service

  web_service:
    build:
      context: .
      dockerfile: docker/web-dev/Dockerfile
    container_name: web_service
    hostname: web_service
    ports:
      - '8000:8000'
    networks:
      - backend
    tty: true
    volumes:
      - $PWD:/home

networks:
  backend:
    driver: 'bridge'

[docker/web-dev/Dockerfile]

FROM python:3.6.5

COPY Pipfile ./home
COPY Pipfile.lock ./home
WORKDIR /home
RUN pip3 install pipenv
RUN pipenv install --system
RUN apt-get update && apt-get install -y vim && apt-get install -y git
RUN pip3 install uwsgi && pip3 install git+https://github.com/Supervisor/supervisor
CMD ["uwsgi", "--ini", "docker/config/uwsgi.ini"]

[码头工人/nginx/Dockerfile]

FROM nginx:latest

COPY . ./home
WORKDIR home
RUN rm /etc/nginx/conf.d/default.conf
COPY ./docker/config/nginx.conf /etc/nginx/conf.d/default.conf

[nginx.conf]

upstream django {
    server web_service:8001;
}
server {
    listen 80;
    server_name 127.0.0.1;
    charset utf-8;

    location / {
        uwsgi_pass django;
        include /etc/nginx/uwsgi_params;
    }

    location /media {
        alias /home/example/media;
    }

    location /static {
        alias /home/example/static;
    }
}

[uwsgi.ini]

[uwsgi]

chdir = /home
module = example.wsgi:application
master = true
process = %(%k * 3)
socket = 127.0.0.1:8001
vaccum = true
chmod-socket=664

我将 nginx '/' 设置为 django 并且 django 正在通过端口 8000 进行侦听。

uwsgi 也在通过 8001 监听并连接 nginx 和 django。

但是当我连接到时http://localhost,它会抛出上游错误。

Web 浏览器中显示错误 -> 502 Bad gateway

泊坞窗日志中显示错误->

connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.0.1, server: 127.0.0.1, request: "GET / HTTP/1.1", upstream: "uwsgi://172.31.0.3:8001", host: "localhost"

我在谷歌搜索了几个小时,但找不到任何方法。

这里有什么解决办法吗?

谢谢。

标签: djangodockernginxuwsgi

解决方案


推荐阅读