关于nginx中使用“Sticky”模块

       使用NGINX做负载均衡器时,常遇到如何将来自同一用户的访问始终定向到一台后端设备进行响应,一般nginx上面有下面办法来实现会话保持:

      1、ip_hash   nginx原生支持的基于IP地址来将不同的请求转发到同一台服务器进行响应,缺点就是如果前端用户都来自同一局域网,基于ip的负载方法会导致负载不均衡;

     2、sticky      基于cookie来进行负载转发,保证将来自同一cookie的访问始终定向到同一服务器响应,缺点就是需要编译模块,而且,cookie需要浏览器支持。

     ip_hash这里不做介绍了,使用也比较简单,最近项目上在用,也是使用了sticky模块。

     sticky模块下载,提供了zip、bz2、gz格式的包,按需自己下载即可:https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/downloads/?tab=tags

#我这里使用1.2.6的zip包

unzip nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.zip

#因为解压出来的名称太长了,所以改个名

mv nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d nginx-sticky-module

#安装OpenSSL及openssl-devel
yum -y install openssl openssl-devel

#重新编译nginx,我这里因为是测试,所以安装目录比较随意,也没有指定别的模块,若线上环境,最好先备份nginx目录

./configure --prefix=/root/asop/nginx1142  --add-module=/root/asop/nginx-sticky-module

make && make install  

    执行make的时候发现有报错,如下

关于nginx中使用“Sticky”模块_第1张图片

处理办法则是:


#进入sticky模块的目录编辑ngx_http_sticky_misc.c

vim ngx_http_sticky_misc.c

新增两行
#include 
#include 

然后保存,重新编译nginx即可。

关于nginx中使用“Sticky”模块_第2张图片

编译安装nginx以后,在nginx.conf 的upstream中使用sticky 引入即可。

upstream asop {
       sticky  expires=1h domain=nw.155.com ;
       server  172.17.8.203:8080;
       server  172.17.8.204:8080;
    }

   sticky模块支持的参数:

sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] 
       [hash=index|md5|sha1] [no_fallback] [secure] [httponly];
name: the name of the cookies used to track the persistant upstream srv; default: route

domain: the domain in which the cookie will be valid default: nothing. Let the browser handle this.

path: the path in which the cookie will be valid default: /

expires: the validity duration of the cookie default: nothing. It's a session cookie. restriction: must be a duration greater than one second

hash: the hash mechanism to encode upstream server. It cant' be used with hmac. default: md5

md5|sha1: well known hash
index: it's not hashed, an in-memory index is used instead, it's quicker and the overhead is shorter Warning: the matching against upstream servers list is inconsistent. So, at reload, if upstreams servers has changed, index values are not guaranted to correspond to the same server as before! USE IT WITH CAUTION and only if you need to!
hmac: the HMAC hash mechanism to encode upstream server It's like the hash mechanism but it uses hmac_key to secure the hashing. It can't be used with hash. md5|sha1: well known hash default: none. see hash.

hmac_key: the key to use with hmac. It's mandatory when hmac is set default: nothing.

no_fallback: when this flag is set, nginx will return a 502 (Bad Gateway or Proxy Error) if a request comes with a cookie and the corresponding backend is unavailable.

secure enable secure cookies; transferred only via https

httponly enable cookies not to be leaked via js

name   默认cookie使用的名称,默认为route

domain:作用域   

path:作用的URL,默认为/

expires:cookie的有效期。

no_fallback:当后端响应的服务器挂掉以后,直接显示502,不到别的服务器进行响应,一般不使用。

secure:安全的cookies,需要https。

httponly:大概意思是cookies不能通过js泄露。

 

 

 

 

 

 

你可能感兴趣的:(web服务那些事)