📜 ⬆️ ⬇️

Binding session to server in nginx. Nginx-sticky-module

Sticky session is a load balancing method in which client requests are sent to the same server in a group.

The easiest way to assign user sessions to a specific server in Nginx is to use the ip-hash method:

upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; } 

When using this method, requests are distributed across servers based on client IP addresses. The first three octets of the client's IPv4 address or the entire client's IPv6 address are used as the key for hashing. The method guarantees that requests of the same client will always be transmitted to the same server. If this server is considered unavailable, then the requests of this client will be transferred to another server (s) . Its disadvantages from the balancing method include: session support problems when a client uses a dynamic IP; not equilibrium balancing, if a large number of requests, for example, passes through one proxy server. Using cookies solves these problems.
')
In Nginx, there is a sticky method that uses a cookie for balancing, though only in the commercial version. There is a more free way - using external modules.

Nginx-sticky-module

The module creates a cookie - which makes each browser unique - and then uses it to redirect requests to the same server. If there is no cookie, for example, on the first request, the server is randomly selected. The project has been forked, the original version is no longer supported, the supported branch is called nginx-sticky-module-ng and is located here . Both links are given because at google request “sticky session nginx” first in the list, after links to the official nginx website, it turns out that the link is exactly to the original project website. And if you, like me, didn’t pay attention to the barely noticeable subtitle written in capital letters and bold: DEPRECATED and below the link to the supported version - before installing the module you will need to make some changes to the source code. The fact is that since the version of nginx 1.5.8, the ngx_sock_ntop () nginx method of the nginx method has been changed , so the nginx-sticky-module file has the ngx_http_sticky_misc.c archive
 digest->len = ngx_sock_ntop(in, digest->data, len, 1); 

worth replacing with
 digest->len = ngx_sock_ntop(in, sizeof(struct sockaddr_in), digest->data, len, 1); 

To install this module, you need to compile Nginx with this module. To do this , if not, install the C / C ++ compiler and the libraries used by nginx (for RedHat / CentOS):
 yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel 

Download the latest version of Nginx sources , unpack it into an inaccessible place, find the nrx-sticky-module or nginx-sticky-module-ng archive in the unpacked src folder and then decide on the nginx options you need to compile
 ./configure --other-install-options --add-module=/path/to/name-of-folder-with-nginx-sticky-module make && make install 

init.d script can be found here , which you need to copy to the file:
 vi /etc/init.d/nginx 

and give him run rights
 chmod a+x /etc/init.d/nginx 

after which you can use the service commands and configure the automatic launch after a reboot:
 # nginx service nginx start #  nginx   chkconfig nginx on 

Configuring a sticky session is no more difficult than for the ip_hash method:
 upstream backend { sticky; server backend1.example.com; server backend2.example.com; } 

By default, a cookie will be created with the name route and a lifetime of 1 hour. The method can take several arguments that can be found on the sites of the modules.

For lovers of perversions and do without third-party modules, you can use the sticky session setting presented here .

PS: additions, comments, comments, suggestions, complaints, parting words are welcome.

Source: https://habr.com/ru/post/231523/


All Articles