Tag Archives: sticky

Nginx: sticky sessions

If you have several backends and your clients should get access to only one of them during their session, you need in sticky sessions.
In case of nginx you have 2 ways:
1) Buy nginx plus
2) Build nginx from source with sticky sessions module

In this article we'll how to for 2nd variant.

Download latest stable sources from http://nginx.org/ru/download.html
Download module from https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/overview
Unpack all.
Setup necessary packages for building from sources

1
apt-get install libpcre3-dev build-essential libssl-dev gcc

cd to folder with nginx sources:

1
./configure --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_gunzip_module --with-http_gzip_static_module --add-module=../nginx-goodies-nginx-sticky-module-ng-f2adff04b8e3

where .../nginx-goodies-nginx-sticky-module-ng-f2adff04b8e3 path to module

1
2
3
make -j6
sudo checkinstal
sudo dpkg -i /home/alter/nginx/nginx-1.6.2/nginx_1.6.2-1_amd64.deb

where /home/alter/nginx/nginx-1.6.2/nginx_1.6.2-1_amd64.deb — path to deb file checkinstall generated for you.
Nginx will be installed in /usr/local/nginx/
If you have configs from previous nginx in /etc/nginx, you could make symlink for configs:

1
rm /usr/local/nginx/config/nginx.conf && ln -sf /etc/nginx/nginx.conf /usr/local/nginx/config/nginx.conf

Your upstream section should looks like:

1
2
3
4
5
6
upstream backends {
    sticky; # this is necessary option.

    server web1.domain.net:80 fail_timeout=60 max_fails=10;
    server web2.domain.net:80 fail_timeout=60 max_fails=10;
}