大网数据广告活动
服务器运维问答

Sticky sessions with Nginx proxy

· 52CE 我爱测

I have an application that's running on two different AWS instances and I'd like to enable "sticky" or "persistent" sessions based on IP so that I can take advantage of web socket technologies in a particular way. I have two diff…

问题原文

原帖链接 · Himmel · CC BY-SA 3.0

I have an application that's running on two different AWS instances and I'd like to enable "sticky" or "persistent" sessions based on IP so that I can take advantage of web socket technologies in a particular way.

I have two different setups that both involve using ip_hash to enable these sticky sessions.

In the first setup, the app processes are running on the same instance as the Nginx config. This is working, the sessions are persistent as expected.

upstream my_app {
    ip_hash;
    # local servers
    server 127.0.0.1:3001 weight=100 max_fails=5 fail_timeout=300;
    server 127.0.0.1:3002 weight=100 max_fails=5 fail_timeout=300;
    keepalive 8;
}

In the second setup, I'm pointing to external instances and trying to achieve the same effect. This set up is not working. In other words, the sessions are still being load balanced.

upstream my_app {
    ip_hash;
    # external servers
    server 111.11.11.11:3001 weight=100 max_fails=5 fail_timeout=300;
    server 222.22.22.22:3002 weight=100 max_fails=5 fail_timeout=300;
    keepalive 8;
}

Am I using ip_hash correctly? How can I enable "sticky" ip-based sessions for external servers?

社区高分解答

原帖链接 · Mr. bug · CC BY-SA 3.0

According to Nginx documentation Sticky session support is only available for their expensive Plus version. I've been researching alternatives and the closer I've been is this old fork that is not compatible with Nginx 1.5+ https://github.com/lusis/nginx-sticky-module

I have also tried to build a LUA module but there are no API hooks for peer selection, just for enumeration and blocking.

Nginx Plus Load Balancing

Update

I've found another great module, see https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-02-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。