简体   繁体   English

使用 nginx 开源的粘性会话负载均衡器

[英]Sticky session load balancer with nginx open source

What the main difference between the sticky session available in nginx plus and hashing cookie in open source version? nginx plus 中可用的粘性会话和开源版本中的散列 cookie 之间的主要区别是什么?

According to the docs nginx open source allows session persistence based on hashing different global variables available within nginx, including $cookie_根据文档nginx 开源允许基于散列 nginx 中可用的不同全局变量的会话持久性,包括$cookie_

With the following configuration:使用以下配置:

    upstream myserver {
        hash $cookie_sessionID;
        server localhost:8092;
        server localhost:8093;
        server localhost:8094 weight=3;
    }

    location / {
       proxy_pass http://myserver;
    }

Assuming, there will be centralized mechanism across backends for generating unique sessionID cookie for all new requests, so what the main disadvantages of such method compared to the nginx plus sticky session approach?假设,会有跨后端的集中机制来为所有新请求生成唯一的 sessionID cookie,那么与 nginx 加粘性会话方法相比,这种方法的主要缺点是什么?

IP Hash load‑balancing can work as "sticky sessions", but you have to keep in mind that this load balancing method is working relative bad itself, because a lot of user/devices shared same external IP-Address in modern world. IP Hash 负载平衡可以作为“粘性会话”工作,但您必须记住,这种负载平衡方法本身的工作效果相对较差,因为在现代世界中,许多用户/设备共享相同的外部 IP 地址。

We experimented with a rather heavily loaded (thousands of parallel users) application and observed tens of percent imbalance between servers when using IP Hash.我们试验了一个负载相当高(数千个并行用户)的应用程序,并观察到在使用 IP 哈希时服务器之间存在百分之几十的不平衡。

Theoretically, the situation should improve with increasing load and number of servers, but for example we did not see any significant difference when using 3 and 5 servers.从理论上讲,随着负载和服务器数量的增加,情况应该会有所改善,但例如,在使用 3 台和 5 台服务器时,我们没有看到任何显着差异。

So, I would strongly advise against using IP Hash in productive environment.因此,我强烈建议不要在生产环境中使用 IP Hash。

As open-source based sticky sessions solution, not bad idea to use HAProxy, because HAProxy support it out-of-the-box.作为基于开源的粘性会话解决方案,使用 HAProxy 是个好主意,因为 HAProxy 支持它开箱即用。 Or HAProxy + Nginx bundle, where HAProxy is responsible for "sticky sessions".或者 HAProxy + Nginx 包,其中 HAProxy 负责“粘性会话”。 (I know about one extremely loaded system that successfully uses such a bundle for this very purpose, so, this is working idea.) (我知道有一个非常负载的系统,它成功地将这样的包用于这个目的,所以,这是一个可行的想法。)

Your approach will work.你的方法会奏效。 According to the official NGINX documentation ( Configuring Basic Session Persistence ):根据官方 NGINX 文档( 配置基本会话持久性):

"If your application requires basic session persistence (also known as sticky sessions), you can implement it in NGINX Open Source with the IP Hash load‑balancing algorithm." “如果您的应用程序需要基本的会话持久性(也称为粘性会话),您可以使用 IP 哈希负载平衡算法在 NGINX 开源中实现它。”

While NGINX Plus "offers a more sophisticated form of session persistence".而 NGINX Plus“提供了一种更复杂的会话持久性形式”。 For example "Least Time" method – when for each request the server with the lowest average latency and the lowest number of active connections is chosen.例如,“最少时间”方法——当为每个请求选择平均延迟最低和活动连接数最少的服务器时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM