代码之家  ›  专栏  ›  技术社区  ›  billy

配置负载均衡器以路由到实例的不同页面?

  •  1
  • billy  · 技术社区  · 7 年前
    • 实例x运行于 端口3000。实例x有两个页面,即x/abc和x/zyx。
    • 目前x的负载均衡器有两个侦听器,即。 80 -> 3000 8080 -> 3000 /

    要求 想向x/zyx发送http请求。

    如何配置LB以路由到特定页面,例如x/abc和x/zyx? 或者用不同的方式写我的请求?

    // url is the DNS of load balancer... this should go to x/abc(?)
    request({
        url: "LoadBalancer-11122232.us-west-2.elb.amazonaws.com:80",
        method: "POST",
        json: true,  
        body: tweetJSON
    }
    

    代码2:服务器2想向x/zyx发出http请求

    // url is the DNS of load balancer... this should go to x/abc 
    // DO I EVEN NEED TWO DIFFERENT PORT LISTENERS(?)
        request({
            url: "LoadBalancer-11122232.us-west-2.elb.amazonaws.com:8080",
            method: "POST",
            json: true,  
            body: tweetJSON
        }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Matt D    7 年前

    您没有将负载平衡器配置为将请求路由到不同的端点,这更像是反向代理的工作,如 Nginx .

    负载平衡器提供一个要调用的端点,并将来自客户端的请求转发到多个相同服务器中的一个。它的目标是跨多个服务器共享高负载。

    代码1:服务器1想要向x/abc发出http请求

    // url is the DNS of load balancer plus the route (/abc)
    request({
        url: "https://LoadBalancer-11122232.us-west-2.elb.amazonaws.com/abc",
        method: "POST",
        json: true,  
        body: tweetJSON
    }
    

    // url is the DNS of load balancer plus the route (/zyx)
    request({
        url: "https://LoadBalancer-11122232.us-west-2.elb.amazonaws.com/zyx",
        method: "POST",
        json: true,  
        body: tweetJSON
    }
    

    如果需要阻止客户端访问后端url,则需要某种形式的身份验证来标识服务器2。

        2
  •  0
  •   adamrights    7 年前

    通常使用负载平衡器来平衡两个节点服务器之间的通信量。

    有关设置,请按照 these Amazon instructions

    在“侦听器”选项卡上,使用箭头查看 侦听器,然后选择添加规则。指定规则如下:

    对于路径模式,请指定用于基于路径的

    更多信息, see Listener Rules.

    一个更便宜的替代方案可能是使用Nginx推出自己的负载平衡器。您可以使用Nginx配置的AMI启动EC2实例。

    然后编辑Nginx配置,使其看起来像这样:

    # The Node.js application serving ABC
    
    upstream node_abc {
      server ip.address.node.instance1:3000;
    }
    
    # The Node.js application serving XYZ 
    upstream node_xyz {
      server ip.address.node.instance2:3000;
    }
    
    # Accept connections from clients
    server {
      listen 80;
      charset utf-8;
      location /abc {
          proxy_pass http://node_abc;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      }
      location /xyz {
        proxy_set_header X-Forwarded-Proto $scheme;
      }
    }
    

    不过,更好的是,我认为您真正想要的是,使用nginx作为一个适当的负载平衡反向代理。为此,您需要运行同一node.js应用程序的两个副本,每个副本都可以响应routes/abc或/xyz,并为页面提供服务器。然后使用如下配置:

    #Proper Load Balanced Nginx setup
    
    upstream node_server {
      server ip.address.node.instance1:3000;
      server ip.address.node.instance2:3000;
    }
    
    # Accept connections from clients
    
    server {
      listen 80;
      charset utf-8;
      location / {
          proxy_pass http://node_server;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      }
    

    您可以在不同的服务器上获得两个不同的节点实例的好处。因此,如果您的一个节点实例宕机,那么负载平衡器将使用另一个节点实例(将/\u运行状况路由添加到nodejs应用程序,该应用程序以200(确定)响应

    您可以很容易地进行A/B测试,蓝绿部署只使用新代码更新一个实例,然后再更新另一个实例。

    推荐文章