Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何配置Nginx进行负载平衡_Python_Docker_Nginx_Flask_Docker Compose - Fatal编程技术网

Python 如何配置Nginx进行负载平衡

Python 如何配置Nginx进行负载平衡,python,docker,nginx,flask,docker-compose,Python,Docker,Nginx,Flask,Docker Compose,我正在学习使用Nginx进行负载平衡的基础知识,下面是一个示例应用程序,包含3个containes 2个app和1个Nginx app1和app2分别在127.0.0.1:5001和127.0.0.1:5002可访问,但当我在127.0.0.1:8081调用nginx时,我收到错误502坏网关 Nginx容器日志 以下是相关文件 应用程序1和应用程序2: 应用程序1: 应用程序2: nginx.conf docker-compose.yml 应用程序中的容器位于网络中它们自己的IP地址上,可以通

我正在学习使用Nginx进行负载平衡的基础知识,下面是一个示例应用程序,包含3个containes 2个app和1个Nginx

app1和app2分别在127.0.0.1:5001和127.0.0.1:5002可访问,但当我在127.0.0.1:8081调用nginx时,我收到错误502坏网关

Nginx容器日志

以下是相关文件

应用程序1和应用程序2:

应用程序1:

应用程序2:

nginx.conf

docker-compose.yml


应用程序中的容器位于网络中它们自己的IP地址上,可以通过容器名称来解析

我重新创建了您的案例,只是做了一些小的修改,因为您没有分解nginx构建,所以我使用了ubuntu容器并修改了net1,由docker compose创建并连接到nginx容器。安装ping和netcat工具后:

为了进一步测试,我使用nc连接应用程序

root@d29b2a96a843:/# nc app1 5000
GET /

"App #1"
^C
root@d29b2a96a843:/# nc app2 5000
GET /

"App #1"
^C
root@d29b2a96a843:/# nc localosht 5001
nc: getaddrinfo for host "localosht" port 5001: Name or service not known
root@d29b2a96a843:/# nc localosht 5002
nc: getaddrinfo for host "localosht" port 5002: Name or service not known
因此,总而言之,您的ngingx容器中的端口5001上没有运行app1,除非您有一些未共享的net1的奇特设置。这就是为什么nginx在连接到上游时抛出错误no live upstreams,因为它在提供的localhost 127.0.0.1上找不到打开的端口5001和5002

对于要使其工作的示例,最简单的方法是修改nginx.conf配置,如下所示:

http {

    upstream loadbalancer {
        server app1:5000 weight=6;
        server app2:5000 weight=4;
    }

    server {
        location / {
            proxy_pass http://loadbalancer/;
        }
    }

}

events {
  
}
version: '3.3'

services:
  
  # Application 1
  app1:
    build: ./app1         
    image: "app1:latest"   
    container_name: app1
    ports:
      - "5001:5000"
    networks:
      - net1
  
  # Application 2
  app2:
    build: ./app2
    image: "app2:latest"
    container_name: app2
    ports:
      - "5002:5000"
    networks:
      - net1

  # Nginx
  nginx:
    build: ./nginx
    image: "nginx:latest"
    container_name: nginx 
    ports:
      - "8081:80"
    depends_on:
      - app1
      - app2
    networks:
      - net1

networks:
  net1:
    external: true
CONTAINER ID   IMAGE                                 COMMAND                  CREATED         STATUS                     PORTS                    NAMES
d29b2a96a843   ubuntu:20.04                          "/bin/bash"              3 minutes ago   Up 3 minutes               0.0.0.0:8081->80/tcp     nginx
0200de2170a0   test-ng                               "python3 app.py"         3 minutes ago   Up 3 minutes               0.0.0.0:5001->5000/tcp   app1
0f1bd788a962   test-ng                               "python3 app.py"         3 minutes ago   Up 3 minutes               0.0.0.0:5002->5000/tcp   app2
root@d29b2a96a843:/# ping app1
PING app1 (172.21.0.2) 56(84) bytes of data.
64 bytes from app1.test_net1 (172.21.0.2): icmp_seq=1 ttl=64 time=0.126 ms
64 bytes from app1.test_net1 (172.21.0.2): icmp_seq=2 ttl=64 time=0.103 ms 
^C
--- app1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.103/0.114/0.126/0.011 ms
root@d29b2a96a843:/# ping app2
PING app2 (172.21.0.3) 56(84) bytes of data.
64 bytes from app2.test_net1 (172.21.0.3): icmp_seq=1 ttl=64 time=0.107 ms
64 bytes from app2.test_net1 (172.21.0.3): icmp_seq=2 ttl=64 time=0.109 ms
^C
--- app2 ping statistics ---
root@d29b2a96a843:/# nc app1 5000
GET /

"App #1"
^C
root@d29b2a96a843:/# nc app2 5000
GET /

"App #1"
^C
root@d29b2a96a843:/# nc localosht 5001
nc: getaddrinfo for host "localosht" port 5001: Name or service not known
root@d29b2a96a843:/# nc localosht 5002
nc: getaddrinfo for host "localosht" port 5002: Name or service not known
http {

    upstream loadbalancer {
        server app1:5000 weight=6;
        server app2:5000 weight=4;
    }

    server {
        location / {
            proxy_pass http://loadbalancer/;
        }
    }

}

events {
  
}