Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways in which Nginx implements session persistence

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the ways of Nginx to achieve session persistence, the article is very detailed, has a certain reference value, interested friends must read it!

Session persistence based on ip_hash

When doing Nginx load balancer, you can set ip_hash in upstream. Each request is allocated according to the hash result of accessing the ip and mapped to a fixed server. When the backend server goes down, the session will be lost. When the request is initiated again, it will revisit another normal server and achieve session persistence. The disadvantage is that the same IP client always accesses a back-end server, which may lead to load imbalance. The following is the session persistence format for ip_hash.

It is assumed that all the back-end servers are running normally.

Configure in the Nginx proxy server (load balancer server): = upstream test {ip_hash; server 10.20.151.112virtual 80; server 10.20.151.113virtual 80;}

As for why this result is returned here, there are specific configuration operations in my Nginx load balancing blog, which you can take a look at if you are interested. Therefore, it is not difficult to see that when I use ip_hash, I achieve session persistence, that is, the client will regularly access the 112 back-end server (unless this server is down), and even if the page is refreshed again, it will not return the content of other back-end servers (Note: in actual production, the content returned by the back-end server to the request client is the same, only to test the effect).

Suppose the server with regular access is down.

Session persistence based on cookie

This method is to store the user's session in the cookie. When the user is assigned to a different server, first judge whether the user's session exists in the server. If not, first store the sessoin in the cookie into the server to achieve session session persistence. The disadvantage is that there are security risks in storing cookie, for example, hackers may get your cookie and get your relevant information. To achieve session persistence in this way, you need to add a sticky_cookie_insert module, which is different from ip_hash in that it does not judge the client based on IP, but on cookie.

Add a sticky module (Nginx that I installed in yum)

Yum install-y pcre* openssl* gcc gcc-c++ make-- install the compilation environment wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip-- download the sticky module nginx-v-- View the Nginx version Because you want to download the source package wget http://nginx.org/download/nginx-1.18.0.tar.gzyum install-y unzip corresponding to the yum installation nginx-- install the decompression tool unzip 08a395c66e42.zip-- extract the module package mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky-module-ng/-- rename tar xzvf nginx-1.18.0.tar.gz-C / usr/local/-- decompress the nginx source Code package cd / usr/local/nginx-1.18.0/nginx-V-View yum install nginx all modules =. / 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 installNginx-V-- View the Nginx module again Added successfully

Configure the proxy server (load balancing server)

Vim upstream.conf-create upstream.conf=upstream qfedu {server 192.168.198.143; server 192.168.198.145; sticky;} vim proxy.conf-create proxy.conf=server {listen 80; server_name localhost; location / {proxy_pass http://testweb; in subprofile conf.d }} nginx-t-- check the configuration file syntax for errors nginx-s reload-- reload the configuration file

Visit http://10.20.151.240/

These are all the contents of the article "what are the ways for Nginx to achieve session persistence?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report