57人参与 • 2024-09-08 • Nginx
ip_hash
,每个请求按访问ip的hash结果分配,映射到固定某一台的服务器#ip_hash语法: upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com down; }
//编译安装sticky模块 //给yum安装的nginx添加模块(1.24最新的版本重新编译安装会报错,1.18的不会报错,版本兼容性的问题) //安装编译环境 [root@nginx-server ~]# yum install -y pcre* openssl* gcc gcc-c++ make [root@nginx-server ~]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip //下载sticky模块 [root@nginx-server ~]# nginx -v nginx version: nginx/1.18.0 [root@nginx-server ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz #下载yum安装nginx对应版本的源码包 [root@nginx-server ~]# yum install -y unzip //安装解压工具 [root@nginx-server ~]# unzip 08a395c66e42.zip //解压模块包 [root@nginx-server ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky-module-ng/ [root@nginx-server ~]# tar xzvf nginx-1.18.0.tar.gz -c /usr/local/ //解压nginx的源码包 [root@nginx-server ~]# cd /usr/local/nginx-1.18.0/ [root@nginx-server nginx-1.18.0]# nginx -v //查看yum安装nginx所有模块 [root@nginx-server nginx-1.18.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=/root/nginx-sticky-module-ng [root@nginx-server nginx-1.18.0]# make && make install //配置基于cookie会话保持 [root@nginx-server conf.d]# vim upstream.conf upstream jianglt { server 192.168.221.136; server 192.168.221.138; sticky; } server { listen 80; server_name www.test.com; charset utf-8; #access_log /var/log/nginx/host.access.log main; location / { proxy_pass http://jianglt; } } [root@nginx-server conf.d]# nginx -t [root@nginx-server conf.d]# nginx -s reload //或者: upstream jianglt { server 192.168.221.136; server 192.168.221.138; sticky expires=1h domain=testpm.com path=/; }
说明:
expires
:设置浏览器中保持cookie的时间domain
:定义cookie的域path
:为cookie定义路径浏览器测试访问;记得在windows本地hosts文件中添加解析
浏览器具体的体现:
1、访问www.test.com,会随机访问到一个页面,可能是136,也可能是138
2、按下f12,打开浏览器的控制台工具,找到请求头下面的cookie(例如:cookie:route=19c3afb04a79d36869450dfe7dca8512),并将cookie复制粘贴到记事本中
3、停掉当前访问的服务器,crtl + f5强制刷新,再次查看浏览器中请求头的cookie,观察是否跟前一次的cookie一样(如果第一次访问的是136的页面,则停掉136,反之,停掉138)
会话保持在web应用中具有重要的作用,主要体现在以下几个方面:
到此这篇关于nginx会话保持的具体实现的文章就介绍到这了,更多相关nginx会话保持内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论