Nginx – Sticky sessions based on a cookie using nginx

apache-2.2nginxtomcat

Currently, we are using apache2 as frontend, and tomcat as backend. We are using mod_proxy_balancer and AJP. Also we are using stickysession by JSESSIONID cookie:

<Proxy balancer://backend>
     BalancerMember ajp://127.0.0.1:8008 min=10 max=100 ping=5 connectiontimeout=40 ttl=60   retry=20 route=node-1
     BalancerMember ajp://127.0.0.1:8009 min=10 max=100 ping=5 connectiontimeout=40 ttl=60 retry=20 route=node-2
     ProxySet lbmethod=byrequests timeout=30
     ProxySet stickysession=JSESSIONID|jsessionid nofailover=Off
</Proxy>

and using jvmRoute parameter in web.xml to add tail to JSESSIONID cookie:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="node-1">

We are looking to switch to using nginx for our frontend. So far I haven't found any way to perform the same session affinity operation with nginx. Is there any solution for this? We are not using session replication, so getting sequential requests to the same backend is crucial.

Best Answer

Google suggests that there is an out-of-tree module for nginx that does this. I've never used it, since I consider sticky sessions to be the work of satan, but it'll be the first option I'd use if I had to implement it.