Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sticky session with apache web server and tomcat servers

I am using apache web server as a load balancer for two tomcat instances behind apache. When the first request goes to node A and second request from the same client goes to node B, i cant access session variables within node A. It's obvious. I surfed in the internet and found that enabling sticky sessions would help. But all the tutorials for enabling the sticky sessions in apache look confusing. Is there any simple step-by-step tutorial for this? Please help.

Code fragment from comment:

ProxyPass /balancer-manager ! 
ProxyPass /balancer://mycluster/ stickysession=JSESSIONID 
ProxyPassReverse /balancer://mycluster/ 
<Proxy balancer://mycluster>; 
  BalancerMember ajp://localhost:9001/ route=NodeA1000 retry=10 
  BalancerMember ajp://localhost:9002/ route=NodeB1000 retry=10 
</Proxy> 
like image 239
ihavprobs Avatar asked May 16 '11 13:05

ihavprobs


People also ask

Can you run Apache and Tomcat on the same server?

Yes. Apache HTTPd can delegate to Apache Tomcat using ModProxy or ModAJP, and can be configured to do so based on the domain, path or file extension requested. Your Apache HTTPd configuration of PHP would remain the same.

What is sticky session in Tomcat?

Tomcat provides in-memory session replication through a combination of serializable session attributes, "sticky sessions", which are provided by the load balancer, and specialized components configured in Tomcat's XML configuration files.

Can HTTP load balancer provide sticky sessions?

Application Load Balancers support both duration-based cookies and application-based cookies. Sticky sessions are enabled at the target group level. You can use a combination of duration-based stickiness, application-based stickiness, and no stickiness across your target groups.


Video Answer


2 Answers

This worked for me...

Instead of using stickysession=JSESSIONID in ProxyPass directive it has to be set within balancer configuration using ProxySet stickysession=JSESSIONID:

<Proxy balancer://mybalancer>
BalancerMember ajp://server1:8009 route=tomcat1
BalancerMember ajp://server2:8009 route=tomcat2
ProxySet lbmethod=bytraffic
ProxySet stickysession=JSESSIONID
</Proxy>
ProxyPass /myapp/ mybalancer://myapp/

It was not working for me when I was using it in ProxyPass as shown below:

ProxyPass /myapp/ mybalancer://myapp/ stickysession=JSESSIONID

This should be added to apache docs, because it's such a pain to solve.

like image 121
Radoslav Lang Avatar answered Sep 21 '22 12:09

Radoslav Lang


For apache httpd to keep your sessions tied to the same backend, it needs to know which cookie keeps the session ID. For java, this is (usually) JSESSIONID.

If you're using the ProxyPass directive, use

ProxyPass /example http://backend.example.com stickysession=JSESSIONID

To be found in the excellent apache httpd documentation.

like image 20
Joeri Hendrickx Avatar answered Sep 21 '22 12:09

Joeri Hendrickx