Nginx實現會話保持的兩種方式
前言
在我們做Nginx負載均衡的時候經常會遇到會話保持的問題,為了保證同一用戶session會被分配到同一臺服務器上,這時就需要會話保持,我們常用的方法有基于ip_hash的會話保持、基于cookie的會話保持。
一、基于ip_hash的會話保持
在做Nginx的負載均衡時,可以在upstream里設置ip_hash,每個請求按訪問ip的hash結果分配,映射到固定某一臺的服務器,當后端服務器宕機后,session會丟失,再次發(fā)起請求時,會重新固定訪問另一臺正常的服務器并實現會話保持。缺點就是由于同一個IP客戶端都固定訪問一個后端服務器,這就可能會導致負載不均衡。下面是ip_hash的會話保持格式。
這里假設后端服務器都正常運行
在Nginx代理服務器(負載均衡服務器)中配置:===========================================upstream test { ip_hash; server 10.20.151.112:80; server 10.20.151.113:80;}

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

二、基于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 --安裝編譯環(huán)境 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模塊,添加成功

在代理服務器(負載均衡服務器)配置
vim upstream.conf --在子配置文件conf.d中創(chuàng)建upstream.conf
=====================================================================================
upstream qfedu {
server 192.168.198.143;
server 192.168.198.145;
sticky;
}
vim proxy.conf ----在子配置文件conf.d中創(chuàng)建proxy.conf
=====================================================================================
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://testweb;
}
}
nginx -t --檢查配置文件語法是否有錯 nginx -s reload --重新加載配置文件
訪問http://10.20.151.240/

總結
Nginx會話保持一般有基于ip_hash和基于cookie兩種方式,盡管Nginx的會話保持可以使某個ip客戶端訪問固定的后端服務器,但這可能會導致負載的不均衡。采用cookie的方式進行會話保持時,需要引入第三方模塊(sticky模塊)才能實現。 使用sticky_cookie_insert啟用會話親緣關系,這會導致來自同一客戶端的請求被傳遞到一組服務器的同一臺服務器,這種方法可以避免上述ip_hash中來自同一局域網的客戶端和前段代理導致負載失衡的情況。 使用后端服務器自身通過相關機制保持session同步,如:使用數據庫、redis、memcached 等做session復制。
到此這篇關于Nginx實現會話保持的兩種方式的文章就介紹到這了,更多相關Nginx 會話保持內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

