django + uwsgi + docker,同时连接到上游

时间:2019-02-07 10:43:54

标签: django docker nginx uwsgi

我通过docker建立了基础架构。

要部署django,请使用uwsgi和nginx。

这是我的配置文件。

[docker-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"]

[docker / 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错误的网关

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

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"

我在Google中搜索了几个小时,但找不到任何方式。

这里有什么解决办法吗?

谢谢。

0 个答案:

没有答案