日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

nginx会话保持之sticky模块

發(fā)布時間:2023/12/29 综合教程 24 生活家
生活随笔 收集整理的這篇文章主要介紹了 nginx会话保持之sticky模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//nginx會話保持之nginx-sticky-module模塊

在使用負載均衡的時候會遇到會話保持的問題,常用的方法有:
1.ip hash,根據客戶端的IP,將請求分配到不同的服務器上;
2.cookie,服務器給客戶端下發(fā)一個cookie,具有特定cookie的請求會分配給它的發(fā)布者,
注意:cookie需要瀏覽器支持,且有時候會泄露數據

1.Sticky工作原理 :

Sticky是nginx的一個模塊,它是基于cookie的一種nginx的負載均衡解決方案,通過分發(fā)和識別cookie,來使同一個客戶端的請求落在同一臺服務器上,默認標識名為route
1.客戶端首次發(fā)起訪問請求,nginx接收后,發(fā)現請求頭沒有cookie,則以輪詢方式將請求分發(fā)給后端服務器。
2.后端服務器處理完請求,將響應數據返回給nginx。
3.此時nginx生成帶route的cookie,返回給客戶端。route的值與后端服務器對應,可能是明文,也可能是md5、sha1等Hash值
4.客戶端接收請求,并保存帶route的cookie。
5.當客戶端下一次發(fā)送請求時,會帶上route,nginx根據接收到的cookie中的route值,轉發(fā)給對應的后端服務器。

1.//下載依賴環(huán)境

[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

##保持原先環(huán)境、將原先模塊找出,用于編譯安裝

[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

總結

以上是生活随笔為你收集整理的nginx会话保持之sticky模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。