zoukankan      html  css  js  c++  java
  • nginx会话保持之sticky模块

    //nginx会话保持之nginx-sticky-module模块

    在使用负载均衡的时候会遇到会话保持的问题,常用的方法有:
    1.ip hash,根据客户端的IP,将请求分配到不同的服务器上;
    2.cookie,服务器给客户端下发一个cookie,具有特定cookie的请求会分配给它的发布者,
    注意:cookie需要浏览器支持,且有时候会泄露数据

    1.Sticky工作原理 :

    Sticky是nginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上,默认标识名为route
    1.客户端首次发起访问请求,nginx接收后,发现请求头没有cookie,则以轮询方式将请求分发给后端服务器。
    2.后端服务器处理完请求,将响应数据返回给nginx。
    3.此时nginx生成带route的cookie,返回给客户端。route的值与后端服务器对应,可能是明文,也可能是md5、sha1等Hash值
    4.客户端接收请求,并保存带route的cookie。
    5.当客户端下一次发送请求时,会带上route,nginx根据接收到的cookie中的route值,转发给对应的后端服务器。

    1.//下载依赖环境

    [root@lb02 ~]# yum install gcc gcc-c++ automake autoconf install pcre-devel openssl-devel -y

    2.//下载与系统相同的nginx源码包

    检查nginx版本系统

    [root@lb02 ~]# nginx -v
    nginx version: nginx/1.14.0

    nginx官网下载对应的源码包

    [root@lb02 ~]# mkdir -p /tools/
    [root@lb02 ~]# cd /tools/

    [root@lb02 tools]# wget http://nginx.org/download/nginx-1.14.0.tar.gz

    3.下载安装sticky模块

    下载官网为: https://github.com

    ##模块下载

    [root@lb02-6 tools]# wget https://github.com/bymaximus/nginx-sticky-module-ng/archive/master.zip

    ###解压改名

    [root@lb02 tools]# unzip master.zip

    [root@lb02 tools]# mv nginx-sticky-module-ng-master nginx-sticky-module-ng

    4.编译安装nginx

    [root@lb02 tools]# tar xf nginx-1.14.0.tar.gz

    ##保持原先环境、将原先模块找出,用于编译安装

    [root@lb02 tools]# nginx   -V

    编译时在最后加入  --add-module=/tools/nginx-sticky-module-ng   如下:

    [root@lb02 tools]#cd nginx-1.14.0/

    [root@lb02 nginx-1.14.0]#./configure   --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/tools/nginx-sticky-module-ng

    [root@lb02 nginx-1.14.0]#make &&   make   install

    [root@lb02 nginx-1.14.0]#echo  $?

    0

    5.配置负载均衡

    //在upstream层添加sticky   如下:

    upstream  blog   {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    sticky;
    }

    //写入之后页面不在轮询

    sticky模块参考
    sticky cookie srv_id expires = 1h domain = .example.com path = /;
    具体使用方法参考官方文档:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#sticky

  • 相关阅读:
    37.leetcode11_container_with_most_water
    36.leetcode8_string_to_integer
    34.leetcode15&5_time_limit_exceeded
    35.leetcode15_3Sum
    33.leetcode6_zigzag_conversion
    32.leetcode3_longest_substring_without_repeating_characters
    31.leetcode2_add_two_numbers
    29.leetcode172_factorial_trailing_zeroes
    30.leetcode171_excel_sheet_column_number
    [LeetCode] 43.Multiply Strings 字符串相乘
  • 原文地址:https://www.cnblogs.com/fangdecheng/p/9857390.html
Copyright © 2011-2022 走看看