配置负载均衡器以路由到实例的不同页面?
Configuring Load Balancer to Route to different pages of instance?
- 我在实例 x 前面有一个 AWS 负载均衡器。
- 实例 x 运行于
端口 3000。实例 x 有两个页面,即 x/abc 和 x/zyx.
- 当前 x 的负载均衡器有两个侦听器,即
80 -> 3000
和 8080 -> 3000
。然后 ping /
Requirement: I has two servers that want to communicate to instance x. Server 1 wants to send http request to x/abc and server 2
wants to send http request to x/zyx.
如何配置 LB 以路由到特定页面,例如x/abc 和 x/zyx? 或者以不同的方式写我的请求?
代码 1:服务器 1 想要向 x/abc
发出 http 请求
// 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
}
通常您使用负载均衡器来平衡两个节点服务器之间的流量。
所以你有两个非常简单的选择。一种是不使用经典的 Amazon 负载均衡器 (ELB),而是改用 ALB(应用程序负载均衡器)。
如需设置,请遵循 these Amazon instructions。
具体注意最后一段:
On the Listeners tab, use the arrow to view the rules for the
listener, and then choose Add rule. Specify the rule as follows:
For Target group name, choose the second target group that you
created.
For Path pattern specify the exact pattern to be used for path-based
routing (for example, /img/*).
更多信息,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 应用程序的两个副本,其中每个副本都可以响应路由 /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;
}
如果您执行最后一项配置,那么您就不必担心在您的页面上进行任何复杂的 url 重写。
您可以从不同服务器上的两个不同节点实例中获益。因此,如果您的一个节点实例出现故障,那么您的负载均衡器将使用另一个节点实例。 (将 /_health 路由添加到以 200 OK 响应的 nodejs 应用程序)
您可以轻松地进行 A/B 测试,并且蓝绿部署仅使用新代码更新一个实例,然后再更新另一个实例。
Nginx 可以针对不同的负载平衡策略进行配置,默认为循环法。如果您需要在用户开始会话后将用户发送到同一节点实例,您还可以添加粘性会话。
您没有配置负载均衡器将请求路由到不同的端点,这更多是反向代理的工作,例如 Nginx。
Load Balancer 提供单个端点来调用,并将来自客户端的请求转发到许多相同服务器之一。 objective 它可以跨多台服务器分担高负载。
在您的情况下,您仍然可以混合使用负载均衡器,但就路由而言,我建议您完整地解决 URL:
代码 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
}
代码 2:服务器 2 想要向 x/zyx
发出 http 请求
// 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。
- 我在实例 x 前面有一个 AWS 负载均衡器。
- 实例 x 运行于 端口 3000。实例 x 有两个页面,即 x/abc 和 x/zyx.
- 当前 x 的负载均衡器有两个侦听器,即
80 -> 3000
和8080 -> 3000
。然后 ping/
Requirement: I has two servers that want to communicate to instance x. Server 1 wants to send http request to x/abc and server 2 wants to send http request to x/zyx.
如何配置 LB 以路由到特定页面,例如x/abc 和 x/zyx? 或者以不同的方式写我的请求?
代码 1:服务器 1 想要向 x/abc
发出 http 请求// 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
}
通常您使用负载均衡器来平衡两个节点服务器之间的流量。
所以你有两个非常简单的选择。一种是不使用经典的 Amazon 负载均衡器 (ELB),而是改用 ALB(应用程序负载均衡器)。
如需设置,请遵循 these Amazon instructions。
具体注意最后一段:
On the Listeners tab, use the arrow to view the rules for the listener, and then choose Add rule. Specify the rule as follows:
For Target group name, choose the second target group that you created.
For Path pattern specify the exact pattern to be used for path-based routing (for example, /img/*).
更多信息,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 应用程序的两个副本,其中每个副本都可以响应路由 /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;
}
如果您执行最后一项配置,那么您就不必担心在您的页面上进行任何复杂的 url 重写。
您可以从不同服务器上的两个不同节点实例中获益。因此,如果您的一个节点实例出现故障,那么您的负载均衡器将使用另一个节点实例。 (将 /_health 路由添加到以 200 OK 响应的 nodejs 应用程序)
您可以轻松地进行 A/B 测试,并且蓝绿部署仅使用新代码更新一个实例,然后再更新另一个实例。
Nginx 可以针对不同的负载平衡策略进行配置,默认为循环法。如果您需要在用户开始会话后将用户发送到同一节点实例,您还可以添加粘性会话。
您没有配置负载均衡器将请求路由到不同的端点,这更多是反向代理的工作,例如 Nginx。
Load Balancer 提供单个端点来调用,并将来自客户端的请求转发到许多相同服务器之一。 objective 它可以跨多台服务器分担高负载。
在您的情况下,您仍然可以混合使用负载均衡器,但就路由而言,我建议您完整地解决 URL:
代码 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
}
代码 2:服务器 2 想要向 x/zyx
发出 http 请求// 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。