【问题标题】:Correct nginx.conf loadbalancing to uvicorn FastAPI with docker-compose使用 docker-compose 将 nginx.conf 负载平衡更正为 uvicorn FastAPI
【发布时间】:2020-04-09 10:23:03
【问题描述】:

我想使用 nginx 作为我的 FastAPI 副本的负载平衡器,但我无法让它工作。我读到 uvicorn 也可以,但是 nginx 可以很好地处理负载平衡。 forum post.

我收到一个错误

host not found in upstream "inconnect1:5001"

docker-compose.yml

version: "3"

networks:
  proxy-tier:
    external:
      name: nginx-proxy

services:

  inconnect1: 
    image: inconnect:0.1
    container_name: inconnect1
    environment:
      - PORT=5001
    volumes:
      - ./inconnect/app:/app
    ports:
      - 5001:5001

  nginx: 
    image: jwilder/nginx-proxy
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./letsencrypt/certs:/etc/nginx/certs:ro
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
    networks:
      - proxy-tier
    restart: always
    deploy:
      mode: replicated
      replicas: 1

nginx.conf

worker_processes 1;

events { worker_connections 1024; }

http {

sendfile on;

upstream restapis {
server inconnect:5001;
}

server {
listen 80;

location / {
  proxy_set_header Host $http_host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_redirect off;
  proxy_buffering off;
  proxy_pass http://restapis;
}

location /static {
  # path for static files
  root /path/to/app/static;
}
}
 }

【问题讨论】:

    标签: nginx docker-compose load-balancing reverse-proxy fastapi


    【解决方案1】:

    作为你的 nginx.conf 文件:

    server inconnect:5001; 应该是server inconnect1:5001;

    并且在 docker compose 文件中应该使用 link 从 nginx 容器到 inconnect1 应用程序。 (移除 nginx 容器上的网络)

        image: jwilder/nginx-proxy
        container_name: nginx
        ports:
          - 80:80
          - 443:443
        volumes:
          - /var/run/docker.sock:/tmp/docker.sock:ro
          - ./letsencrypt/certs:/etc/nginx/certs:ro
          - ./nginx/nginx.conf:/etc/nginx/nginx.conf
        links:
          - "inconnect1:inconnect1"
        restart: always
        deploy:
          mode: replicated
          replicas: 1
    

    或在 1 个网络上使用:

    
    networks:
      proxy-tier:
        driver: bridge
    
    services:
    
      inconnect1: 
        image: inconnect:0.1
        container_name: inconnect1
        environment:
          - PORT=5001
        volumes:
          - ./inconnect/app:/app
        ports:
          - 5001:5001
        networks:
          - proxy-tier
    
      nginx: 
        image: jwilder/nginx-proxy
        container_name: nginx
        ports:
          - 80:80
          - 443:443
        volumes:
          - /var/run/docker.sock:/tmp/docker.sock:ro
          - ./letsencrypt/certs:/etc/nginx/certs:ro
          - ./nginx/nginx.conf:/etc/nginx/nginx.conf
        networks:
          - proxy-tier
        restart: always
        deploy:
          mode: replicated
          replicas: 1
    

    【讨论】:

    • 谢谢。但是,nginx.conf 还不能用于 SSL,可以修改吗?
    猜你喜欢
    • 1970-01-01
    • 2016-11-25
    • 2022-06-13
    • 1970-01-01
    • 2018-09-24
    • 2014-02-04
    • 2022-11-04
    • 1970-01-01
    • 2019-10-11
    相关资源
    最近更新 更多