For those who are not willing to spend some bucks on purchasing nginx plus but your manager insist upon enabling session persistence in nginx, the best option would be check the nginx approved set of modules here –> Nginx 3rd party modules

There is a 3rd party module in that list by the name : Sticky upstream

Download here

 

Obviously, nginx has to be recompiled to enable this 3rd party module (not a dynamic module).

Download the desired version of nginx source code from  here.

If you already have an nginx version running in your server and want to replace it with the new one, check the compile options used to install the old version using the command :

#nginx -V 

Remember to add the option “–with-http_gunzip_module –add-module=<path_to_module_location>” during compilation.

I used the below compile options :

./configure  --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --add-module='/usr/src/nginx-goodies-nginx-sticky-module-ng-08a395c66e42' --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -Wl,--as-needed'

 

(Install required dependencies)

Few of the dependencies are:

yum install gperftools-devel.x86_64
yum install GeoIP-devel.x86_64
yum install perl-ExtUtils-Embed
yum install libxslt*
yum install libxml2
yum install openssl-devel
yum install pcre-devel


-----Installing GD Library -----------
Download latest source file for libgd from : 
https://github.com/libgd/libgd/releases

cd libgd-gd-2.2.5
./bootstrap.sh
./configure
make
make install
-------------------- 
To complete installation of nginx give the below commands: 
make 
make install

 

After installation use “nginx -V” again to check if the new module is listing there.

Once confirmed, edit nginx conf to enable “sticky” directive:

 

Eg:

upstream {
  sticky;
  server 127.0.0.1:9000;
  server 127.0.0.1:9001;
  server 127.0.0.1:9002;
}

(note that this is a load balancing nginx configuration)

 

Sticky directive can have the below switches too:
sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] 
       [hash=index|md5|sha1] [no_fallback] [secure] [httponly];
(how cool is that!)
For the description on each switch check here.

 

Good Luck!

(Please comment and share)