April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Apache Server Load Balancing for Multiple Virtual Hosts Tomcat

Apache Server Load Balancing for Multiple Virtual Hosts Tomcat

 

apache
vi /etc/http/conf/httpd.conf

NameVirtualHost *:443
NameVirtualHost *:80

<VirtualHost *:80>
ServerName server1.rmohan.com
Redirect permanent / https://server1.rmohan.com/
</VirtualHost>

<VirtualHost *:443>
ServerName server1.rmohan.com
SSLProxyEngine On
KeepAlive On

<Proxy balancer://Cluster>
BalancerMember ajp://localhost:18009 disablereuse=On route=jvm1
BalancerMember ajp://localhost:28009 disablereuse=On route=jvm2
</Proxy>

ProxyPass / balancer://Cluster/ stickysession=JSESSIONID
</VirtualHost>

<VirtualHost *:80>
ServerName server2.rmohan.comm
Redirect permanent / https://server2.rrmohan.com/
</VirtualHost>

<VirtualHost *:443>
ServerName server2.rmohan.com
SSLProxyEngine On
KeepAlive On

<Proxy balancer://Cluster>
BalancerMember ajp://localhost:18009 disablereuse=On route=jvm1
BalancerMember ajp://localhost:28009 disablereuse=On route=jvm2
</Proxy>

ProxyPass / balancer://Cluster/ stickysession=JSESSIONID
</VirtualHost> [/apache]

And these VirtualHost blocks go on for as many servers as needed. I know it doesn’t look pretty and in fact it’s incorrect since the VirtualHost for SSL connection shouldn’t have a ServerName directive. Not unless you’re using the SNI with mod_gnutls or some specific combination of server and browser like Apache 2.2.12 or later and Mozilla Firefox 2.0 or later. In my environment it did work and so this was used for a while. The first improvement on the configuration that I attempted looked like this:

[apache] NameVirtualHost *:80

<VirtualHost *:80>
ServerName server1.rmohan.com
Redirect permanent / https://server1.rmohan.com/
</VirtualHost>

<VirtualHost *:80>
ServerName server2.domain.com
Redirect permanent / https://server2.rmohan.com/
</VirtualHost>

KeepAlive On

<Proxy balancer://wwwCluster>
BalancerMember ajp://localhost:18009 route=jvm1
BalancerMember ajp://localhost:28009 route=jvm2
</Proxy>

NameVirtualHost *:443

<VirtualHost *:443>
ProxyPass / balancer://Cluster/ stickysession=JSESSIONID
</VirtualHost>

[/apache]

At first it semed ok, but then I noticed that it produced a strange side-effect.
The balancer worked, but the redirect from HTTP to HTTPS didn’t. Therefore this configuration was not acceptable.
Thinking about it some more I realized that I don’t actually need to know the name of the host when redirecting it to my backend server because in my case it will process the Host header and provide the appropriate result. Googling for the redirection to HTTPS alternative I found this page which suggests that Apache’s RewriteEngine could be used to achieve this result. So here’s my final configuration that does the redirect to HTTPS, load balancer’s sticky sessions are functioning properly and everything gets redirected to one of the backend servers chosen by the balancer regardless of the hostname that was used to access it. In other words it works the way I need it to.

[apache]

NameVirtualHost *:80

<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

ProxyPass / balancer://Cluster/ stickysession=JSESSIONID

<Proxy balancer://Cluster>
BalancerMember ajp://localhost:18009 route=jvm1
BalancerMember ajp://localhost:28009 route=jvm2
</Proxy>

NameVirtualHost *:443

<VirtualHost *:443>
SSLEngine on
</VirtualHost>

[/apache]

And if you’re using Apache server with PHP rather than JAVA I found this article to be a good read on setting up load balancer with sticky sessions for PHP.

ProxyPass / balancer://cluster/ lbmethod=bytraffic

BalancerMember http://server1.rmohan.com:8080/
BalancerMember http://server2.rmohan.com:8080/
BalancerMember http://server3.rmohan.com:8080/

ProxyPassReverse / http://www.rmohan.com/

 

2 comments to Apache Server Load Balancing for Multiple Virtual Hosts Tomcat

  • Greetings from California! I’m bored to tears at work so I decided to browse your blog on my iphone during lunch break. I really like the info you provide here and can’t wait to take a look when I get home. I’m amazed at how quick your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyways, good site!

  • just stumbled upon your blog and wanted to say that i have really enjoyed reading your blog posts. any way i’ll be subscribing to your feed and i hope you post again soon

Leave a Reply to Alternative to V8 Fusion Cancel reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>