Jump to: navigation, search

Difference between revisions of "Jetty/Howto/Configure Virtual Hosts"

< Jetty‎ | Howto
m
Line 10: Line 10:
 
You can set virtual hosts in several different ways, including:
 
You can set virtual hosts in several different ways, including:
  
* Using a context XML file in the context directory: [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/handler/ContextHandler.html#setVirtualHosts(java.lang.String[] setVirtualHosts]. This is the preferred method.
+
* Using a context XML file in the context directory: [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/handler/ContextHandler.html#setVirtualHosts setVirtualHosts]. This is the preferred method.
 
* Java calls in an embedded usage: [http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty Embedding Jetty].
 
* Java calls in an embedded usage: [http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty Embedding Jetty].
 
* Within an explicitly deployed webapp (no deployer) listed in jetty.xml or similar.
 
* Within an explicitly deployed webapp (no deployer) listed in jetty.xml or similar.
 
* Usng a WEB-INF/jetty-web.xml file (deprecated, but works with the webapp deployer if you do not use the context deployer).  
 
* Usng a WEB-INF/jetty-web.xml file (deprecated, but works with the webapp deployer if you do not use the context deployer).  
  
The examples that follow set virtual hosts in the preferred way, by calling the method ContextHandler.setVirtualHost.
+
The examples that follow set virtual hosts in the preferred way, by calling the method ContextHandler.setVirtualHost [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/handler/ContextHandler.html#setVirtualHosts setVirtualHosts].
  
 
==Configuring virtual hosts==
 
==Configuring virtual hosts==

Revision as of 11:37, 10 August 2010



Introduction

A virtual host is an alternative name, registered in DNS, for an IP address. Virtual hosting takes one of two forms:

  • Multiple names can resolve to a single IP address.
  • Multi-homed hosts, that is machines with more than one network interface, can have a different name for each IP address.

Jetty users often want to configure their web applications taking into account these different virtual hosts. Frequently, a machine with a single IP address has different DNS resolvable names associated with it, and a webapp deployed on it must be reachable from all of the alternative names. Another possibility is to serve different web applications from different virtual hosts. Let's examine these possibilities.

You can set virtual hosts in several different ways, including:

  • Using a context XML file in the context directory: setVirtualHosts. This is the preferred method.
  • Java calls in an embedded usage: Embedding Jetty.
  • Within an explicitly deployed webapp (no deployer) listed in jetty.xml or similar.
  • Usng a WEB-INF/jetty-web.xml file (deprecated, but works with the webapp deployer if you do not use the context deployer).

The examples that follow set virtual hosts in the preferred way, by calling the method ContextHandler.setVirtualHost setVirtualHosts.

Configuring virtual hosts

When configuring a web application, you can supply a list of IP addresses and names at which the web application will be reachable. Suppose we have a machine with these IP addresses and DNS resolvable names:

  • 333.444.555.666
  • 127.0.0.1
  • www.blah.com
  • www.blah.net
  • www.blah.org

Suppose we have a webapp, xxx.war that we want to be served from all of the above names and addresses. Then we would configure the webapp like so:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/xxx</Set>
  <Set name="war"><SystemProperty name="jetty.home"/>/webapps/xxx.war</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>333.444.555.666</Item>
      <Item>127.0.0.1</Item>
      <Item>www.blah.com</Item>
      <Item>www.blah.net</Item>
      <Item>www.blah.org</Item>
    </Array>
  </Set>
</Configure>

Assuming we had configured a connector listening on port 8080, then webapp xxx.war would be available at all of the following addresses:

  • http://333.444.555.666:8080/xxx
  • http://127.0.0.1:8080/xxx
  • http://www.blah.com:8080/xxx
  • http://www.blah.net:8080/xxx
  • http://www.blah.org:8080/xxx

Configuring different webapps for different virtual hosts

This is accomplished simply by supplying a different list of virtual hosts for each webapp. For example, suppose our imaginary machine has these DNS names and IP addresses:

  • 333.444.555.666
  • 127.0.0.1
  • www.blah.com
  • www.blah.net
  • www.blah.org
  • 777.888.888.111
  • www.other.com
  • www.other.net
  • www.other.org

Suppose also we have another webapp, zzz.war. We want xxx.war to be deployed as above, and zzz.war to be deployed only from 777.888.888.111, www.other.com, www.other.net and www.other.org:

<!-- webapp xxx.war -->
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/xxx</Set>
  <Set name="war"><SystemProperty name="jetty.home"/>/webapps/xxx.war</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>333.444.555.666</Item>
      <Item>127.0.0.1</Item>
      <Item>www.blah.com</Item>
      <Item>www.blah.net</Item>
      <Item>www.blah.org</Item>
    </Array>
  </Set>
</Configure>


<!-- webapp zzz.war -->
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/zzz</Set>
  <Set name="war"><SystemProperty name="jetty.home"/>/webapps/zzz.war</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>777.888.888.111</Item>
      <Item>www.other.com</Item>
      <Item>www.other.net</Item>
      <Item>www.other.org</Item>
    </Array>
  </Set>
</Configure>

Webapp xxx.war is still available at:

  • http://333.444.555.666:8080/xxx
  • http://127.0.0.1:8080/xxx
  • http://www.blah.com:8080/xxx
  • http://www.blah.net:8080/xxx
  • http://www.blah.org:8080/xxx

But now webapp zzz.war is available at:

  • http://777.888.888.111:8080/zzz
  • http://www.other.com:8080/zzz
  • http://www.other.net:8080/zzz
  • http://www.other.org:8080/zzz

Configuring different webapps for different virtual hosts, but at the same context path

In our example above, we have made webapp zzz.war avilable not only at a certain set of virtual hosts, but also at the context path /zzz, whilst our other webapp is available at both a different set of virtual hosts, and at a different context path. What happens if we want them at the same context path, but still at different sets of virtual hosts?

Very simply, we just supply the same context path for each webapp, leaving the disjoint set of virtual host definitions as before:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="war"><SystemProperty name="jetty.home"/>/webapps/xxx.war</Set>
  <Set name="contextPath">/</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>333.444.555.666</Item>
      <Item>127.0.0.1</Item>
      <Item>www.blah.com</Item>
      <Item>www.blah.net</Item>
      <Item>www.blah.org</Item>
    </Array>
  </Set>
</Configure>
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="war"><SystemProperty name="jetty.home"/>/webapps/zzz.war</Set>
  <Set name="contextPath">/</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>777.888.888.111</Item>
      <Item>www.other.com</Item>
      <Item>www.other.net</Item>
      <Item>www.other.org</Item>
    </Array>
  </Set>
</Configure>

Now, webapp xxx.war is available at:

  • http://333.444.555.666:8080/
  • http://127.0.0.1:8080/
  • http://www.blah.com:8080/
  • http://www.blah.net:8080/
  • http://www.blah.org:8080/

and webapp zzz.war is available at:

  • http://777.888.888.111:8080/
  • http://www.other.com:8080/
  • http://www.other.net:8080/
  • http://www.other.org:8080/

Configuring virtual hosts with non-ascii characters

International domain names are names containing non-ascii characters. For example "http://www.bücher.com". The DNS internally remains based on ascii, so these kinds of names are translated via an encoding called punycode into an ascii representation. Modern browsers detect these non-ascii characters in URLs and automatically apply the punycode encoding. For example, typing this url into a browser:

http://www.åäö.com:8080/test/

is translated to the following url:

http://www.xn--4cab6c.com:8080/test/

For using internationalized domain names with jetty virtual hosts, you need to supply the punycoded form of the name in your context xml file (and of course you will need to supply it to your DNS setup).

Here's an example. Say I'm running a webapp on port 8080 at context /test, and I want to configure a virtual host for www.åäö.com. I configure its ascii equivalent in the context xml file for the context:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 
  <Set name="contextPath">/</Set>
  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set>
 
  <Set name="virtualHosts">
    <Array type="String">
      <Item>www.xn--4cab6c.com</Item>
    </Array>
  </Set>
</Configure>

After starting jetty, I will be able to enter the url http://www.åäö.com:8080/test/ in my browser and reach my webapp.

Note that if I don't have any webapps deployed at /, hitting the url http://www.åäö.com:8080 will hit jetty's default handler, which serves back a 404 page listing the available contexts:

<H2>Error 404 - Not Found.</H2>
No context on this server matched or handled this request.<BR>
Contexts known to this server are: 
<ul><li><a href="http://www.xn--4cab6c.com:8080/test">/test @ www.xn--4cab6c.com:8080 ---> WebAppContext@82d210@82d210/test,file:/tmp/Jetty_0_0_0_0_8080_test.war__test_www.xn..4cab6c.com_1jadjg/webapp/,/home/janb/src/jetty-eclipse/jetty/trunk/jetty-distribution/target/distribution/webapps/test.war</a></li>
</ul>

You'll notice that the link already has the punycode transformed domain name in it.