In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Nginx session persistence based on nginx-sticky-module module". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Nginx session persistence based on nginx-sticky-module module".
Sticky introduction
Both sticky module and Ip_hash are related to load balancing algorithm, but there are differences between them.
Ip hash, which assigns requests to different servers according to the IP of the client
Sticky. According to the cookie given by the server to the client, the client will wear this cookie,nginx when it requests again and forward the request with this cookie to the server that issued the cookie.
Sticky principle
Sticky is a load balancing solution based on cookie, which distributes and identifies cookie so that requests from the same client fall on the same server. The default cookie ID is route:
The client initiates an access request for the first time, and after nginx receives it, it finds that there is no cookie in the request header, and then distributes the request to the back-end server by polling.
The back-end server processes the request and returns the response data to nginx.
At this point, nginx generates a cookie with route and returns it to the client. The value of route corresponds to the backend server, which may be plaintext or hash values such as md5, sha1, etc.
The client receives the request and saves the cookie with route.
When the client sends a request next time, it will bring route,nginx and forward it to the corresponding backend server according to the value of route in the received cookie.
Sticky official website
Official address: https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src download address: https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
Pay attention
If the same client initiates multiple requests at the same time, it may fall on different back-end servers.
Because cookie is originally issued by the server, if the client disables cookie, the cookie will not take effect.
When a client sends a request without a cookie,Android client, it usually does not take all the cookie with it. You need to specify which cookie will carry it. If you want to use sticky for load balancing, please add cookie to Android development.
The cookie name should not be the same as the cookie used by the business. The default cookie name of Sticky is route, which can be changed to any value
The first request sent by the client is without cookie. The cookie sent by the server will not take effect until the next request from the client.
Nginx sticky module cannot be used with ip_hash
Nginx installs Sticky module
If you haven't deployed Nginx yet, do it when you deploy Nginx-- just add this module to add-module. I've already installed Nginx here, and you need to load this module into NGINX again.
1. Download sticky
Wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gztar xf master.tar.gz# put this module in the nginx/module directory. The name is too long. Rename mkdir / usr/local/nginx/modulemv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 / usr/local/nginx/module/nginx-sticky-module.
two。 Recompile NGINX download a NGINX and decompress it again, and then look at the modules compiled by NGINX before, all of which are added here, and add-- add-module loads the sticky module at the end
Tar xf nginx-1.16.1.tar.gzcd nginx-1.16.1./configure-- prefix=/usr/local/nginx\-- sbin-path=/usr/local/nginx/sbin/nginx\-- conf-path=/usr/local/nginx/conf/nginx.conf\-- pid-path=/usr/local/nginx/run/nginx.pid\-- error-log-path=/usr/local/nginx/logs/error.log\-http-log-path=/usr/local/nginx/logs / access.log\-- with-pcre\-- user=nginx\-- group=nginx\-- with-stream\-- with-threads\-- with-file-aio\-- with-http_v2_module\-- with-http_ssl_module\-- with-http_realip_module\-- with-http_gzip_static_module\-- with-http_stub_status_module\-- add-module=/usr/local/nginx/module/nginx-sticky-module # in The sticky module #. / configure is loaded and compiled. Then change the nginx program make mv / usr/local/nginx/sbin/nginx / usr/local/nginx/sbin/nginx.oldcp-rf objs/nginx / usr/local/nginx/sbin/# and finally make upgrade for update detection [root@nginx_proxy02 nginx-1.16.1] # make upgrade/usr/local/nginx/sbin/nginx-tnginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf Test is successfulkill-USR2 `cat / usr/local/nginx/run/ nginx.pid`sleep 1test-f / usr/local/nginx/run/nginx.pid.oldbinkill-QUIT `cat / usr/local/nginx/run/ nginx.pid.oldbin`
This completes the loading of Nginx into third-party modules and uses nginx-V to see if the loading is successful.
3. Modify NGINX configuration file modify NGINX configuration file to enable sticky
Upstream backend {sticky name=ngx_cookie expires=6h; server 192.168.31.240 upstream backend 8080 weight=3 max_fails=3 fail_timeout=10s; server 192.168.31.241 upstream backend 8080 weight=3 max_fails=3 fail_timeout=10s; server 192.168.31.242 weight=3 max_fails=3 fail_timeout=10s; server 192.168.31.242 weight=6 max_fails=3 fail_timeout=10s; server 192.168.31.243 weight=6 max_fails=3 fail_timeout=10s; server 8080 down;} mkdir / usr/local/nginx/ngx_cookie
4.sticky syntax parsing
Directive describes the name of name setting record cookie (customizable). By default, set the domain name to be used by cookie for routedomain. Set the URL path for cookie. By default, the root directory expires sets the lifetime hash value of cookie to index, md5, sha1, corresponding to plaintext, md5, and sha1. Default md5, test sha1 back-end server will change to no_fallback when sticky's back-end machine dies, nginx returns 502 without forwarding to other servers. It is not recommended to set secure to enable secure cookie. HTTPS is required to support httponly to allow cookie not to leak through JS.
* * 5. Restart NGINX * *
/ usr/local/nginx/sbin/nginx-s reload test access to NGINX
There is no Cookie on the first visit. The NGINX will include Cookie in the returned data only after the access is completed, and Cookie will appear the next time the data is requested. So you can see the Cookie after refreshing it.
Thank you for reading, the above is the content of "Nginx session persistence based on nginx-sticky-module module". After the study of this article, I believe you have a deeper understanding of the problem of Nginx session persistence based on nginx-sticky-module module, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.