简体   繁体   中英

Apache as Reverse-Proxy and Load-Balancer need to use sticky session

I have a two CentOS 8 nodes (192.168.1.101 and 192.168.1.102) in cluster (using pacemaker) where I configured VIP (192.168.1.100) and Apache as cluster's resources. On all nodes are installed Apache 2.4 and Tomcat 9

The configuration for Apache is: vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
        ProxyPreserveHost On

        <Proxy balancer://myClusterBalancer>
             BalancerMember http://192.168.1.101:8080/ route=server01 keepalive=On ping=5 max=200 ttl=120
             BalancerMember http://192.168.1.102:8080/ route=server02 keepalive=On ping=5 max=200 ttl=120
             ProxySet stickysession=JSESSIONID|jsessionid lbmethod=byrequests timeout=60
        </Proxy>

        ProxyPass /server-status !
        ProxyPass /MyWebApp/  balancer://myClusterBalancer/MyWebApp/ stickysession=JSESSIONID|jsessionid scolonpathdelim=On
        ProxyPassReverse /MyWebApp/  balancer://myClusterBalancer/MyWebApp/

        ProxyPass / balancer://myClusterBalancer/MikiFax/ stickysession=JSESSIONID|jsessionid scolonpathdelim=On
        ProxyPassReverse / balancer://myClusterBalancer/MyWebApp/
</VirtualHost>

and my Tomcat server.xml for Node1 and Node2 vim /opt/tomcat/conf/server.xml

<Engine name="Catalina" defaultHost="localhost" jvmRoute="server01">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="server02">

My system work in this way:

  1. HTTP request to VIP on http://192.168.1.100/MyWebApp and I receive a reply from Apace;
  2. Apace forward the request to one of the two Tomcat as Reverse Proxy and Load Balancer
  3. Tomcat send http response

Now on the page on Tomcat I have different link and I need when I click on this link (open a new tab) I must use the same Tomcat Server.

The link are in this format:

First Link:

<a target="_blank" href="http://192.168.1.100/MyWebApp/F?URL=1599825050056&amp;viewAttach=Y">CLICK FOR DETAILS</a>

When I click it I open a new tab with this Second Link:

<a href="attachments\myAttachment.pdf" target="_blank">myAttachment.pdf</a>

this file is saved on the server when I load the page (after I click on the first link), and If I click on the link I open the file in new tab, but sometime this request is processed by the other server and I receive an error because on the second server this file was not downloaded.

How can I resolve this problem? Thanks for the update.

I Solved it.

Modify Tomcat server.xml for Server1

<Engine name="Catalina" defaultHost="localhost" jvmRoute="server01">

Modify Tomcat server.xml for Server2

<Engine name="Catalina" defaultHost="localhost" jvmRoute="server02">

Modify Apache conf

<VirtualHost *:80>
        ProxyPreserveHost On
        Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
    
        <Proxy balancer://myClusterBalancer>
            BalancerMember http://192.168.1.101:8080/ route=server01
            BalancerMember http://192.168.1.102:8080/ route=server02
            ProxySet stickysession=ROUTEID
        </Proxy>
        
        ProxyPass /MyWebApp/  balancer://myClusterBalancer/MyWebApp/
        ProxyPassReverse /MyWebApp/  balancer://myClusterBalancer/MyWebApp/
        
        ProxyPass / balancer://myClusterBalancer/MyWebApp/
        ProxyPassReverse / balancer://myClusterBalancer/MyWebApp/
    </VirtualHost>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM