Wednesday, February 25, 2009

Configuring virtual hosts in Jetty

I just figured out how to do virtual hosting in Jetty. I have a customer with two sites. The first site (I will call it webappsite.com) is running a java webapp on the root of the site and has a ROOT.war in the webapps directory. The second site (calling it staticsite.com) is currently just a static site. I wanted the site content for the static site to be easily accessible, so I put it into the user's home directory. Both sites need to run on the same IP number and port number, so it was necessary to use Jetty's virtual hosting features.

The webapp on webappsite.com is automatically deployed from the ROOT.war file. I didn't need to do anything extra in any configuration files for it to work. However, since I wanted a static file area on that site as well, I went ahead and added a configuration file to set that up. This wasn't necessary to make this site work and was only done to be able to serve static files stored outside of the ROOT.war file.

I also added a configuration file for staticsite.com. This was necessary for this site to work. Both of these configuration files were put into Jetty's contexts directory. The file for webappsite.com is called contexts/webappsite.xml (sorry for the horrid layout -- I need to figure out how to better post xml code in blogger):

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<configure class="org.mortbay.jetty.handler.ContextHandler">
<call class="org.mortbay.log.Log" name="debug"><arg>Configure webappsite.com site</arg></call>
<set name="contextPath">/web</set>
<set name="resourceBase"><systemproperty name="user.home" default=".">/sites/webappsite.com/web/</systemproperty>
<set name="handler">
<new class="org.mortbay.jetty.handler.ResourceHandler">
<set name="welcomeFiles">
<array type="String">
<item>index.html</item>
</array>
</set>
<set name="cacheControl">max-age=3600,public</set>
</new>
</set>
<set name="VirtualHosts">
<array type="java.lang.String">
<item>192.168.0.1</item>
<item>webappsite.com</item>
<item>www.webappsite.com</item>
<item>alternatedomainforsite.com</item>
<item>www.alternatedomainforsite.com</item>
</array>
</set>
</set>
</configure>
The static site's config file is called contexts/staticsite.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<configure class="org.mortbay.jetty.handler.ContextHandler">
<call class="org.mortbay.log.Log" name="debug"><arg>Configure staticsite.com site</arg></call>
<set name="contextPath">/</set>
<set name="resourceBase"><systemproperty name="user.home" default=".">/sites/staticsite.com/</systemproperty>
<set name="handler">
<new class="org.mortbay.jetty.handler.ResourceHandler">
<set name="welcomeFiles">
<array type="String">
<item>index.htm</item>
</array>
</set>
<set name="cacheControl">max-age=3600,public</set>
</new>
</set>
<set name="VirtualHosts">
<array type="java.lang.String">
<item>staticsite.com</item>
<item>www.staticsite.com</item>
<item>anotherdomainforstaticsite.com</item>
<item>www.anotherdomainforstaticsite.com</item>
</array>
</set>
</set>
</configure>
So the following mappings are now taking place:
webappsite.com/ --> jetty/webapps/ROOT.war
webappsite.com/web --> sites/webappsite.com/web
staticsite.com/ --> sites/staticsite.com/
This seems to have done the trick!

No comments: