首頁> 運維> Nginx> 主體

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

王林
發布: 2023-05-29 23:15:49
轉載
5208 人瀏覽過

一、基於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實現會話保持的方式有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!