nginx添加sticky cookie 分流模块

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx添加sticky cookie 分流模块相关的知识,希望对你有一定的参考价值。

需要下载nginx源码和sticky,在nginx配置文件中添加sticky模块,然后重新编译nginx。

 

#准备安装基础环境:
yum install gcc openssl-devel pcre-devel zlib-devel

#下载nginx1.6.3
wget http://nginx.org/download/nginx-1.6.3.tar.gz

#解压缩nginx
tar xvf nginx-1.6.3.tar.gz

#安装依赖的库
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
yum -y install gd-devel
yum -y install perl-devel perl-ExtUtils-Embed
yum -y install GeoIP GeoIP-devel GeoIP-data
yum -y install gperftools-devel

#查看nginx的配置
nginx -V

#加入sticky模块
cd nginx-1.6.3

./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-google_perftools_module --with-debug --with-cc-opt=‘-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic‘ --with-ld-opt=‘-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E‘ --add-module=/root/nginx-sticky

#编译
make

#停止nginx
service nginx stop

#替换nginx
cp /usr/sbin/nginx /usr/sbin/nginx.bak.20161009
cp ./objs/nginx  /usr/sbin/nginx


#修改nginx配置:

upstream test_server {
        server 10.2.118.48:8002;
        server 10.2.118.49:8002;
        sticky;
}

 

#重启nginx

service nginx restart

 

参考资料:

https://my.oschina.net/766/blog/156693

http://www.xuebuyuan.com/2184929.html

http://blog.chinaunix.net/uid-24625974-id-2894092.html

以上是关于nginx添加sticky cookie 分流模块的主要内容,如果未能解决你的问题,请参考以下文章

Nginx添加stickky保持会话共享

nginx根据cookie分流

nginx 基于cookie分流

使用nginx sticky实现基于cookie的负载均衡

AbtestingGateway 分流策略添加

nginx cookie 会话保持功能