【问题标题】:reverse proxy nginx docker django反向代理 nginx docker django
【发布时间】:2021-11-13 22:49:23
【问题描述】:

所以我想做的是拥有两个网络应用:appli1 和 appli2。

当我从浏览器访问 x.x.x.x:8000 时,appli1 运行良好,但使用 x.x.x.x:8001 我没有得到 appli2,但我又得到了 appli1。那么我怎样才能让它正常工作呢?

这是我的 docker-compose 文件:

version: '3'

services:
  reverse-proxy:
    image: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 8000:8000
      - 8001:8001
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    depends_on:
      - redis
    environment:
      - REDIS_HOST=redis
    #ports: 
      #- "8000:8000"
  web2:
    build: .
    command: python manage.py runserver 0.0.0.0:8001
    depends_on:
      - redis
    environment:
      - REDIS_HOST=redis
  redis:
    image: redis:3.2-alpine
    volumes:
      - redis_data:/data
volumes:  
  redis_data:
 

这是我的 nginx.conf 文件:

events {

}

http {
  server {
    listen 8000;
    server_name web.appli1.com;

    location / {
      proxy_pass http://web:8000;
    }
  }
  server {
    listen 8001;
    server_name web2.appli2.com;

    location / {
      proxy_pass http://web2:8001;
    }
  }
}

这是我的 dockerfile:

FROM python:3
ENV PYTHONUNBUFFERED 1
WORKDIR /app
ADD . /app
COPY ./requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app
CMD . /app/djangodocker/
      

我还使用主机的 ip 在 /etc/hosts 中设置域

【问题讨论】:

  • 那么你的问题是什么?
  • 请添加您的 Dockerfiles
  • 我已经编辑并添加了 dockerfile

标签: django docker nginx


【解决方案1】:
version: "3.7"
services:

  nginx:
     image: nginx:latest
     container_name: ngnix-proxy
     ports:
       - 8000:8000
     volumes:
       - ./nginx.conf:/etc/nginx/conf.d/default.conf
     depends_on:
       - web
       - web2

server {
  listen 8000;
  root /srv/www/static;

  location / {
    index index.html index.htm;
    try_files $uri $uri/ @web @web2;
  }

  location @web {
    proxy_pass http://web:8000;
  }

  location @web2 {
    proxy_pass http://web2:8001;
  }
}

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 2018-05-31
    • 2018-10-10
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    相关资源
    最近更新 更多