1

I currently have Apache 2.4 integrated with two tomcat servers in a load balancing configuration.

The webserver will take requests from the DNS request for two domain names, http://domain1.nl and http://domain2.nl

I wish to send requests for http(s)://domain1.nl/ to http(s)://domain1.nl/myapp1/login/login.do AND http(s)://domain2.nl/ to http(s)://domain2.nl/myapp2/

myapp1 and myapp2 are both running on both load balanced tomcat instances.

  • How does Apache take DNS requests? I did not know Apache was now a DNS server. – ETL Feb 02 '15 at 17:42
  • possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted How to Know about Mod\_Rewrite Rules but Were Afraid to Ask](http://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – ETL Feb 02 '15 at 17:43
  • I meant to say the DNS server forwards requests for both domains to the Apache 2.4 webserver. My assumption is Apache can differentiate between the URL requests it receives which forms the basis for virtualHosting. I have updated my question to clarify this. – Toby Derrum Feb 02 '15 at 17:47
  • identifying what specifically you think has already been answered would be far more useful than pointing me to a link – Toby Derrum Feb 02 '15 at 17:51
  • DNS servers don't forward HTTP requests either. They resolve host names to IP addresses and vice versa. I think you need to spend a bit more time reading the Apache documentation, understand virtualhost, mod proxy and mod rewrite. – ETL Feb 02 '15 at 17:51
  • you need to do some homework. Server fault is for professional system administrators - that implies you read the manuals, did a lot of home work. Your question is one of the basic fundamental things people do with Apache. It's all over google search. – ETL Feb 02 '15 at 17:54
  • So the DNS server resolves rather than forwards. I appreciate your fundamental clarification though it is succinct in relation to the question I am asking and I do not see how focusing on my faux pas invalidates the question I asked. The abundance of various though none specific references on google can be as much a curse as a blessing and I'm unsure how you concluded I haven't bothered with as much as a simple google search. – Toby Derrum Feb 02 '15 at 18:01

2 Answers2

0

Enable mod_rewrite and 'mod_proxy_http` on Apache.

<VirtualHost *:80>
  ServerName domain1.nl
  Rewriteengine on
  RewriteRule ^/$ http://domain1.nl/myapp1/login/login.do
  ProxyPass /myapp1 http://localhost:8080/myapp1
  ProxyPassReverse /myapp1 http://localhost:8080/myapp1
</VirtualHost>


<VirtualHost *:80>
  ServerName domain2.nl
  Rewriteengine on
  RewriteRule ^/$ http://domain2.nl/myapp2/
  ProxyPass /myapp2 http://localhost:8080/myapp2
  ProxyPassReverse /myapp1 http://localhost:8080/myapp2
</VirtualHost>

There is the copy/paste answer. But that will probably just lead to more questions because you don't get the fundamental understanding by copy/pasting verbatim.

Read all of Apache Virtual Host and Apache Rewrite Module documentation so you actually understand how it works so you can think with this.

To have those URL go through Apache to your backend Tomcat server, apply proxy directives as per the Apache Proxy documentation. Obviously, in the configuration above, localhost should be replaced with whatever host your Tomcat server is on.

ETL
  • 6,443
  • 1
  • 26
  • 47
  • Thanks, I am a developer to whom it has fallen the task of also configuring the webserver. I saw similar solutions pointing in this direction but I unfortunately do not control the DNS server which takes away my preferred choice of tinkering. I have only a short window in which to deploy which is why I needed a degree of confidence around what I intend. I am assuming I can repeat these entries with to cater to HTTPS requests ? – Toby Derrum Feb 02 '15 at 18:17
  • Apache does not support https with different hostnames - as in, you won't be able to specify a different cert for each host so one of the host will give you "invalid host name". – ETL Feb 02 '15 at 18:46
  • Understood. I will include it for just the domain that has the SSL certificate installed. Thanks for your help. – Toby Derrum Feb 02 '15 at 19:13
  • I'm unsure if there is more I need to do but now i've enabled rewrite and the URL successfully forwards, Tomcat does not seem present and I get a 404 page error although I can access my webapp via https at the same URL. Is there some JKMount configuration required ? – Toby Derrum Feb 06 '15 at 13:42
  • See edits on Proxy – ETL Feb 06 '15 at 14:35
  • This doesn't work. I should point out I am already using mod_JK and I have a workers.properties file. Should I not be leveraging this ? – Toby Derrum Feb 09 '15 at 14:26
  • `mod_jk`: up to you, you did not say so originally. The above is for `http proxy`, not `jk`. Whether you should use `jk` or `http` proxy I don't know. I have only used `http` and did not see any draw back but I did not do extensive testing myself. – ETL Feb 09 '15 at 14:28
  • Ok so I would like to use JKMount I am also loading both applications /myapp1 and /myapp2 on both servers. I've seen examples whereby users wish to deploy one app to a specific server but not one that assumes both are running on both. – Toby Derrum Feb 09 '15 at 14:36
  • I figure it out. It involved duplicating (without modification) all my JKmount mappings to my balancer straight into each VirtualHost. – Toby Derrum Feb 09 '15 at 16:24
  • Very good! Glad you were able to expand on this to make it work for your exact environment. – ETL Feb 09 '15 at 16:30
0

Enable mod_rewrite and 'mod_proxy_http` on Apache.

Depending on apache version these mods may not exist (due to 'mod_'), try instead:

a2enmod rewrite
a2enmod proxy_http
service apache2 restart

And in your virtual host, this is what i use to redirect to tomcat and exclude php admin from forwarding:

    ProxyPreserveHost on
    ProxyRequests off
    ProxyPass /phpmyadmin ! 
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
Alpha2k
  • 121
  • 1
  • 5