溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Nginx實現會話保持的方式有哪些

發布時間:2022-03-18 13:33:38 來源:億速云 閱讀:540 作者:小新 欄目:開發技術

這篇文章主要介紹Nginx實現會話保持的方式有哪些,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、基于ip_hash的會話保持

在做Nginx的負載均衡時,可以在upstream里設置ip_hash,每個請求按訪問ip的hash結果分配,映射到固定某一臺的服務器,當后端服務器宕機后,session會丟失,再次發起請求時,會重新固定訪問另一臺正常的服務器并實現會話保持。缺點就是由于同一個IP客戶端都固定訪問一個后端服務器,這就可能會導致負載不均衡。下面是ip_hash的會話保持格式。

這里假設后端服務器都正常運行

在Nginx代理服務器(負載均衡服務器)中配置:===========================================upstream test {   ip_hash;      server 10.20.151.112:80;      server 10.20.151.113:80;}

Nginx實現會話保持的方式有哪些

至于這里為什么會返回這個結果,在我的Nginx實現負載均衡那篇博客有具體配置操作,感興趣的可以去看看。因此不難看出,當我使用ip_hash時,實現了session保持,即客戶端會固定訪問112這臺后端服務器(除非這臺服務器宕機了),就算再次刷新頁面也不會返回其他后端服務器的內容(注意:實際生產中后端服務器返回給請求客戶端的內容是一樣的,這里僅僅是為了做測試效果)。

假設固定訪問的那臺服務器宕機了

Nginx實現會話保持的方式有哪些

二、基于cookie的會話保持

這種方式就是將 用戶的session存入cookie里,當用戶分配到不同的服務器時,先判斷服務器是否存在該用戶的session,如果沒有就先把cookie里面的sessoin存入該服務器,實現session會話保持。缺點是存入cookie有安全隱患,比如黑客可能會獲取你的cookie從而獲取你相關信息。使用這種方式實現會話保持保持,需要添加sticky_cookie_insert模塊,與ip_hash不同之處在于,它不是基于IP來判斷客戶端的,而是基于cookie來判斷。

添加sticky模塊(我用yum方式安裝的Nginx)

yum install -y pcre* openssl* gcc gcc-c++ make   --安裝編譯環境
wget  https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip   --下載sticky模塊
nginx -v  --查看Nginx版本,因為要下載和yum安裝nginx對應版本的源碼包
wget  http://nginx.org/download/nginx-1.18.0.tar.gz
yum install -y unzip   --安裝解壓工具
unzip 08a395c66e42.zip --解壓模塊包
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky-module-ng/  --改名
tar xzvf nginx-1.18.0.tar.gz -C /usr/local/  --解壓nginx的源碼包
cd /usr/local/nginx-1.18.0/
nginx -V   --查看yum安裝nginx所有模塊
======================================================================================
./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=/root/nginx-sticky-module-ng
======================================================================================
make && make install
Nginx -V  --再次查看Nginx模塊,添加成功

Nginx實現會話保持的方式有哪些

在代理服務器(負載均衡服務器)配置

vim upstream.conf   --在子配置文件conf.d中創建upstream.conf
=====================================================================================
upstream qfedu {
        server 192.168.198.143;
        server 192.168.198.145;
        sticky;
}
vim proxy.conf     ----在子配置文件conf.d中創建proxy.conf
=====================================================================================
server {
    listen       80;
    server_name  localhost;
    
    location / {
        proxy_pass http://testweb;
    }
}
nginx -t    --檢查配置文件語法是否有錯
nginx -s reload   --重新加載配置文件

訪問http://10.20.151.240/

Nginx實現會話保持的方式有哪些

以上是“Nginx實現會話保持的方式有哪些”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女