nginx负载均衡器-Docker撰写 [英] nginx load balancer - Docker compose

查看:91
本文介绍了nginx负载均衡器-Docker撰写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的flask应用程序运行在容器内的端口5000上,并且我试图添加nginx负载平衡来缩放该应用程序(3个实例)

I have a simple flask app running on port 5000 inside the container , and i'm trying to add nginx load balance to scale the app(3 instances)

这是我的 docker-compose 文件:

version: "3.7"

services: 
    chat-server:
        image: chat-server
        build: 
            context: .
            dockerfile: Dockerfile
        volumes: 
            - './chat_history:/src/app/chat_history'
        networks: 
            - "chat_net"

    ngnix-server:
        image: nginx:1.13
        ports: 
            - "8080:80"
        volumes: 
            - './ngnix.conf:/etc/ngnix/nginx.conf'
        networks: 
            - "chat_net"
        depends_on: 
            - chat-server

networks: 
    chat_net:

这是我的 nginx.conf 文件:

events { worker_connections 1024;}

http {
    upstream app {
        server chat-server_1:5000;
        server chat-server_2:5000;
        server chat-server_3:5000;

    }
}

server {
    listen 80;
    location / {
        proxy_pass http://app;
    }
}

两个服务都位于同一 chat_net 网络,但是当我在浏览器中点击 localhost:8080 时,却得到了nginx默认页面,为什么呢?我缺少什么?

both services are on the same chat_net network , but when i hit localhost:8080 on my browser im getting the nginx default page , why is that? what am i missing ?

推荐答案

您有错字,没有安装在 nginx.conf 文件正确。

You have a typo and are not mounting in your nginx.conf file correctly.

您在卷部分的几个位置将其拼写为 ngnix 并且该容器使用默认配置运行(因此使用默认主页)。

You spell it ngnix in a couple of places in your volumes section and the container runs with the default config (hence default home page).

一旦解决,您可能会遇到@Federkun提到的错误( nginx 将无法解析这3个域名

Once you fix that, you will probably hit the error mentioned by @Federkun (nginx won't be able to resolve the 3 domain names you're proxying).

您还将 server 指令放置在错误的位置(它必须位于 http 部分)。

You also have your server directive in the wrong place (it needs to be within the http section).

这应该是文件的修改版本:

This should be the modified version of your file:

events { worker_connections 1024;}

http {
    upstream app {
        server chat-server:5000;
    }
    server {
        listen 80;
        location / {
            proxy_pass http://app;
        }
    }
}

请注意,这比需要更好 nginx 注意副本数。您可以使用-scale chat-server = N 运行 docker-compose up 并通过运行相同的命令随时调整大小不使用停机的其他 N 命令。

Notice this is better than needing nginx to be aware of the replica count. You can run docker-compose up with --scale chat-server=N and resize at anytime by running the same command with a different N without downtime.

这篇关于nginx负载均衡器-Docker撰写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆