如何使用nginx代理托管多个django应用程序并通过子域重定向?

kulphzqa  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(160)

我用docker创建代理容器,用jwilder/nginx-proxy生成ssl证书到我的域。它工作正常,但现在尝试通过子域重定向我的django应用程序,每个请求返回502个坏网关。我是一个新手。我需要帮助来知道我做错了什么。
这是我的docker-compose nginx代理:

version: '3'

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - certs:/etc/nginx/certs:ro
      - vhostd:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
    labels:
      - com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    restart: always
    environment:
      - NGINX_PROXY_CONTAINER=nginx-proxy
    volumes:
      - certs:/etc/nginx/certs:rw
      - vhostd:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - /var/run/docker.sock:/var/run/docker.sock:ro

  www:
    image: nginx
    restart: always
    expose:
      - "80"
    volumes:
      - /Users/kbs/git/peladonerd/varios/1/www:/usr/share/nginx/html:ro
    environment:
      - VIRTUAL_HOST=pablokbs.com,www.pablokbs.com
      - LETSENCRYPT_HOST=pablokbs.com,www.pablokbs.com
      - LETSENCRYPT_EMAIL=pablo@pablokbs.com
    depends_on:
      - nginx-proxy
      - letsencrypt
volumes: 
  certs:
  html:
  vhostd:

这是django应用程序(巴克_web应用程序通过子域重定向):

version: "3"

services:
  core_api:
    build: 
      context: .
    env_file: .env
    container_name: "bak-api"
    ports: 
      - 8181:8181
    volumes:
      - ./BAK_API:/bak_api
      - ./bak:/bak_api
    command: uvicorn bak.asgi:app --host 0.0.0.0 --port 8181
  bak_web:
    build:
      context: .
    expose:
      - "80"
    env_file: .env
    container_name: "bak-web"
    volumes:
      - static:/bak_web/static
      - .:/bak_web
      - ./bak_chatbot:/app
  nginx-bak-web:
    image: nginx
    restart: always
    expose:
      - "80"
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - static:/bak_web/static
    environment:
      - VIRTUAL_HOST=bakzion.duckdns.org
      - LETSENCRYPT_HOST=bakzion.duckdns.org
      - LETSENCRYPT_EMAIL=omar.cravioto.p@gmail.com
    depends_on:
      - bak_web

  
  
volumes:
  .:
  static:

最后是local.conf配置:

upstream bakzion.duckdns.org {
    server bak_web:80;
}

server {
    listen 80;
    server_name bakzion.duckdns.org;

    location /static/{
        alias /bak_web/static/;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass uwsgi://webapp.docker.localhost;
        include /etc/nginx/vhost.d/default_location;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        uwsgi_read_timeout 180; 
    }
}

我试着用nginx创建代理docker来重定向服务器中的每个django应用主机

ccrfmcuu

ccrfmcuu1#

你真的需要这个零件吗?:

www:
    image: nginx
    restart: always
    expose:
      - "80"
    volumes:
      - /Users/kbs/git/peladonerd/varios/1/www:/usr/share/nginx/html:ro
    environment:
      - VIRTUAL_HOST=pablokbs.com,www.pablokbs.com
      - LETSENCRYPT_HOST=pablokbs.com,www.pablokbs.com
      - LETSENCRYPT_EMAIL=pablo@pablokbs.com
    depends_on:
      - nginx-proxy
      - letsencrypt

它指向了另一个人的域,也许这是一个冲突。我最近主持了两个网站与jwilder和他们都工作。
我的jwilder配置示例:

nginx-proxy:
    image: bbtsoftwareag/nginx-proxy-unrestricted-requestsize:alpine
    networks:
      - nginx-net
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - certs:/etc/nginx/certs:ro
      - vhostd:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      # If you have default config, user next line: (from a config folder of your project)
      - ./config/default_location:/etc/nginx/vhost.d/default_location
      - myProject_static_myProject:/myProject/static
      - myProject_media_myProject:/myProject/media
      - myProject2_static_myProject2:/myProject2/static
      - myProject2_media_myProject2:/myProject2/media
    labels:
      - com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    restart: always
    environment:
     - NGINX_PROXY_CONTAINER=nginx-proxy
    volumes:
     - certs:/etc/nginx/certs:rw
     - vhostd:/etc/nginx/vhost.d
     - html:/usr/share/nginx/html
     - /var/run/docker.sock:/var/run/docker.sock:ro

volumes: 
  certs:
  html:
  vhostd:
  myProject_static_myProject:
    external: true
  myProject_media_myProject:
    external: true
  myProject2_static_myProject2:
    external: true
  myProject2_media_myProject2:
    external: true

#If you have a network
networks:
  nginx-net:
    name: network_name

正如佩拉多书呆子所说...令人印象深刻!

相关问题