Nginx会话保持模块详解-sticky

Nginx会话保持模块详解-sticky

1、模块简介

官方REDEME:A nginx module to add a sticky cookie to be always forwarded to the same upstream server.
When dealing with several backend servers, it’s sometimes useful that one client (browser) is always served by the same backend server (for session persistance for example).
Using a persistance by IP (with the ip_hash upstream module) is maybe not a good idea because there could be situations where a lot of different browsers are coming with the same IP address (behind proxies)and the load balancing system won’t be fair.

  • 翻译&个人理解:这是一个会话保持模块,用于在浏览器保持cookie,将用户请求始终转发到一组服务器,当有多个后端服务器时,列如一下登录页面,那么会话持久性就很有必要,但cookie粘贴本身存在安全问题,session的最终解决方案,建议在后端进行实现.

1.1、模块下载地址

https://github.com/bymaximus/nginx-sticky-module-ng

  • 注意:目前仅是在nginx-1.10.3下测试可以安装,其他版本请自行测试

2、模块的安装

1
2
3
4
5
6
7
8
9
[root@test0002 nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --without-mail_pop3_module --without-mail_smtp_module --without-mail_imap_module --add-module=/soft/ngx_http_proxy_connect_module-0.0.1/ --add-module=/soft/nginx-sticky-module-ng-1.2.6/
[root@test0002 nginx-1.10.3]# make
[root@test0002 nginx-1.10.3]# make install
[root@test0002 sbin]# ./nginx -V
nginx version: nginx/1.10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --without-mail_pop3_module --without-mail_smtp_module --without-mail_imap_module --add-module=/soft/ngx_http_proxy_connect_module-0.0.1/ --add-module=/soft/nginx-sticky-module-ng-1.2.6/

3、配置语法

3.1、upstream配置方法

sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback] [secure] [httponly];

  • name:设置用来记录会话的cookie名称;
  • domain:哪些域名下可以使用这个cookie;
  • path:设置cookie作用的URL路径,默认根目录;
  • hash=index|md5|sha1:设置cookie中服务器的标识是用明文还是使用md5值,默认使用md5
  • expires:设置cookie的生存期,默认不设置,浏览器关闭即失效,需要是大于1秒的值;
  • no_fallback:如果cookie对应的服务器宕机了,那么将会返回502报错,不建议设置;
  • secure:设置启用安全的cookie,需要HTTPS支持;
  • httponly:允许cookie不通过JS泄漏;
1
2
3
4
5
6
upstream lisir_cluster{
sticky;
server 127.0.0.1:9000;
server 127.0.0.1:9001;
server 127.0.0.1:9002;
}
-------------本文结束感谢您的阅读-------------
LiGuanCheng wechat
如有问题,请与我微信交流或通过右下角“daovoice”与我联系~。
请我喝一杯咖啡~