Domain forwarding

using DNS, Apache and Tomcat

the problem

You have a web application running on Tomcat. Now you obtained a nice domain name and you want to point it to your web application. Or let's make it more interesting: you have 2 web applications running on a single Tomcat instance, and you bought 2 domain names.

Most domain registrars give you the following options to redirect your domain name somewhere else:
  • http redirect
  • domain forwarding, or "cloaking"
  • edit dns records
We'll look at each of these methods now:

http redirect

This method redirects a request for your domain name to the specified resource. The response will contain the location of the destination resource, and and indication if this redirecting is permanent or temporary (HTTP status codes 301 and 302).

Using this method you can redirect to any resource you want, for example to

http://1.2.3.4.5.6:8081/webapplicationname/something
The problem with this method is that your browser will display this destination in its address bar, in lieu of the very nice domain name that you just bought -- a very bad user experience.

domain forwarding ("cloaking")

Using domain forwarding, sometimes called "cloaked forwarding" or "cloaking", a request to your domain name is handled by your domain registrar's server, where it opens an autmatically generated web page that contains an (i)frame that loads your destination resource.

Using this method you can redirect to any resource you want, for example to

http://1.2.3.4.5.6:8081/webapplicationname/something
This method has the advantage that the destination resource is not shown in the browser's address bar. However it has very serious drawbacks. The biggest disadvantage is that you will lose the HTML header info that is in your destination HTML. That means you lose the title, the favicon, any meta tags.. some domain registrars allow you to enter some of this (normally title and meta description) into the frame-forwarding page, but basically you've lost all control.

dns

By editing DNS records you can solve the problems of the above methods, but it comes with some difficulties itself.

The main difficulty is that in DNS A records, you can specify a destination IP address, but nothing else. No port number or web application name there. So how are you going to distinguish on your server between web app A and web app B, when requests coming from domain name A and domain name B arrive at that same IP address ?

The answer lies in using the HTTP "Host" header, that contains the domain name that initiated this HTTP request. When you set up everything correctly, the users will see in their browser's address bar things like

http://www.yourdomainname.com
or
http://yourdomainname.com
and when accessing resources they may see
http://www.yourdomainname.com/something
Exactly what you want! It allows for excellent bookmarking and does not ruin the user experience by showing things like IP addresses, port numbers and web application names. The HTML header of your own page is fully available so you can have your favicon, page title, meta tags and whatever you use, properly sent to the browser.

The rest of this page shows you how to set it up using Apache HTTP server and Tomcat. Many thanks to Jose Garcia who figured out (and implemented on this server) and documented the gory details of the configuration described below.

setting up dns forwarding with apache and tomcat

This description assumes you're doing the DNS trick using the domain name "geonetwork.tv". It assumes that your Tomcat is up and running a web application called "geonetworktv" on a Linux server.

installation

install Apache HTTP server:

apt-get install apache2
install mod_jk:
apt-get install lib-apache-mod-jk

configuration of mod_jk

Edit

/etc/libapache2-mod-jk/workers.properties
so that it contains
workers.tomcat_home=[the path to your tomcat instance]
for example
workers.tomcat_home=/home/pipo/apps/tomcat/apache-tomcat-6.0.18
and
worker.ajp_13worker.port=[the port to talk to tomcat (default is 8009)]
in our example
worker.ajp_13worker.port=9009
and
worker.ajp_13worker.host=[name of virtual host]
in our example
worker.ajp_13worker.host=geonetwork.tv
If you want to define another virtual host (if you have a second web app) you add another worker: in our example
worker.ajp_13worker2.port=9009
worker.ajp_13worker2.host=anotherdomain.com

configuration of Tomcat

If you changed the default ajp13 port, do it here too:

<Connector port="9009" protocol="AJP/1.3" redirectPort="8443"/>
Add a listener in the server section to configure mod_jk in Tomcat:
<server>
...
<Listener className="org.apache.jk.config.ApacheConfig"
 modJk="/usr/lib/apache2/modules/mod_jk.so"
 workersConfig="/etc/libapache2-mod-jk/workers.properties"/>
...
</server>
Define a virtual host for each domain:
<Host name="geonetwork.tv"
 appBase="/home/pipo/apps/tomcat/apache-tomcat-6.0.18/geonetworktv"
 unpackWARs="true"
 autoDeploy="true"
 xmlValidation="false"
 xmlNamespaceAware="false">

   <Context path="" docBase="ROOT" debug="0"/>
   <Alias>www.geonetwork.tv<Alias/>
</Host>
Optionally you can comment away the default Host that's no longer in use:
<!-- Host name="localhost"
</Host -->
Now create the application directory, and deploy. Instead of using Tomcat's default web applications directory in
/home/pipo/apps/tomcat/apache-tomcat-6.0.18/webapps
we will create one application directory per virtual host. In our example:
/home/pipo/apps/tomcat/apache-tomcat-6.0.18/geonetworktv
To make the application accessible through http://geonetwork.tv it's deployed to the default ROOT application in its application directory:
/home/pipo/apps/tomcat/apache-tomcat-6.0.18/geonetworktv/ROOT

configuration of Apache

The Apache default site configuration is in

/etc/apache2/sites-available/default
We must add a new virtual host for each domain we're going to serve:
<VirtualHost *:80>
 ServerName geonetwork.tv
 ServerAlias geonetwork.tv www.geonetwork.tv
 JkMount /* ajp13_worker
<VirtualHost *:80>
to add a second domain use
<VirtualHost *:80>
 ServerName anotherdomain.com
 ServerAlias alias1.com alias2.com
 JkMount /* ajp13_worker 2
<VirtualHost *:80>