一、基于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/