nginx会话保持之sticky模块
//nginx會(huì)話保持之nginx-sticky-module模塊
在使用負(fù)載均衡的時(shí)候會(huì)遇到會(huì)話保持的問(wèn)題,常用的方法有:
1.ip hash,根據(jù)客戶端的IP,將請(qǐng)求分配到不同的服務(wù)器上;
2.cookie,服務(wù)器給客戶端下發(fā)一個(gè)cookie,具有特定cookie的請(qǐng)求會(huì)分配給它的發(fā)布者,
注意:cookie需要瀏覽器支持,且有時(shí)候會(huì)泄露數(shù)據(jù)
1.Sticky工作原理 :
Sticky是nginx的一個(gè)模塊,它是基于cookie的一種nginx的負(fù)載均衡解決方案,通過(guò)分發(fā)和識(shí)別cookie,來(lái)使同一個(gè)客戶端的請(qǐng)求落在同一臺(tái)服務(wù)器上,默認(rèn)標(biāo)識(shí)名為route
1.客戶端首次發(fā)起訪問(wèn)請(qǐng)求,nginx接收后,發(fā)現(xiàn)請(qǐng)求頭沒(méi)有cookie,則以輪詢方式將請(qǐng)求分發(fā)給后端服務(wù)器。
2.后端服務(wù)器處理完請(qǐng)求,將響應(yīng)數(shù)據(jù)返回給nginx。
3.此時(shí)nginx生成帶route的cookie,返回給客戶端。route的值與后端服務(wù)器對(duì)應(yīng),可能是明文,也可能是md5、sha1等Hash值
4.客戶端接收請(qǐng)求,并保存帶route的cookie。
5.當(dāng)客戶端下一次發(fā)送請(qǐng)求時(shí),會(huì)帶上route,nginx根據(jù)接收到的cookie中的route值,轉(zhuǎn)發(fā)給對(duì)應(yīng)的后端服務(wù)器。
1.//下載依賴環(huán)境
[root@lb02 ~]# yum install gcc gcc-c++ automake autoconf install pcre-devel openssl-devel -y
2.//下載與系統(tǒng)相同的nginx源碼包
檢查nginx版本系統(tǒng)
[root@lb02 ~]# nginx -v
nginx version: nginx/1.14.0
nginx官網(wǎng)下載對(duì)應(yīng)的源碼包
[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模塊
下載官網(wǎng)為: 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
##保持原先環(huán)境、將原先模塊找出,用于編譯安裝
[root@lb02 tools]# nginx -V
編譯時(shí)在最后加入--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.配置負(fù)載均衡
//在upstream層添加sticky 如下:
upstream blog {
server 172.16.1.7:80;
server 172.16.1.8:80;
sticky;
}
//寫入之后頁(yè)面不在輪詢
sticky模塊參考
sticky cookie srv_id expires = 1h domain = .example.com path = /;
具體使用方法參考官方文檔:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#sticky
總結(jié)
以上是生活随笔為你收集整理的nginx会话保持之sticky模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微信小程序获取手机验证码
- 下一篇: log4j2配置文件模板(带详细注释)