ihavprobs
ihavprobs

Reputation: 1492

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> 

Upvotes: 19

Views: 69772

Answers (6)

NullIsNot0
NullIsNot0

Reputation: 411

None of the above solutions worked for me, but I found it in MOTECH project documentation here: http://docs.motechproject.org/en/latest/deployment/sticky_session_apache.html

This config works for me (on Apache 2.4.41):

<VirtualHost *:80>
    (...)
    Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
    (...)
    <Proxy balancer://mycluster>
        (...)
        BalancerMember http://10.20.30.40:8080 route=backend-1 enablereuse=On
        BalancerMember http://10.20.30.41:8080 route=backend-2 enablereuse=On

        ProxySet lbmethod=bytraffic
        ProxySet stickysession=ROUTEID
        (...)
    </Proxy>

    ProxyPass / balancer://mycluster/
    ProxyPassReverse / balancer://mycluster/
    (...)
</VirtualHost>

Upvotes: 3

Manish Gupta
Manish Gupta

Reputation: 71

Pls try this, I'm sure this will work for you.

Step-1: Add below code in httpd.conf:

<Proxy balancer://mycluster>
BalancerMember http://<NODE1>/<APP>/  route=jvm1 
BalancerMember http://<NODE2>/<APP>/  route=jvm2
ProxySet lbmethod=bytraffic
ProxySet stickysession=JSESSIONID
</Proxy>

ProxyPass /<APP>/ balancer://mycluster/ 
ProxyPassReverse /<APP>/ balancer://mycluster/

Step-2: Add below code in server.conf:

a) <NODE1>
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">    
b) <NODE2>
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">

Upvotes: 7

amp
amp

Reputation: 1

I think your problem is that you are using mybalancer where you have tu use balancer:

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

Upvotes: 0

Wilb
Wilb

Reputation: 21

This is a problem I have come across too - if you define your balancer within a vhost then it seems to use the stickysession as documented. However if you are defining a balancer outside of the vhost its used in then the stickysession gets lost so you have to set it using ProxySet within the balancer itself.

Upvotes: 2

Radoslav Lang
Radoslav Lang

Reputation: 171

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.

Upvotes: 17

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17435

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.

Upvotes: 11

Related Questions