Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

< Jetty‎ | Howto
(New page: {{Jetty Howto | introduction = A virtual host is an alternative name, registered in DNS, for an IP address. A single IP address may have many such alternative names. Multi-homed hosts, t...)
 
 
(17 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Jetty Howto
 
{{Jetty Howto
 
| introduction =  
 
| introduction =  
A virtual host is an alternative name, registered in DNS, for an IP address. A single IP address may have many such alternative names.
 
  
Multi-homed hosts, that is machines with more than one network interface, may have a different name for each IP address. This is also refered to as "virtual hosting".
+
{{Jetty Redirect|http://www.eclipse.org/jetty/documentation/current/configuring-virtual-hosts.html}}
  
Essentially, "virtual hosting" concerns the resolution of a DNS registered name to an IP address - many names may resolve to the same IP address, and 1 or more IP addresses may reside on the same physical machine.
+
A virtual host is an alternative name, registered in DNS, for an IP address. Virtual hosting takes one of two forms:
  
Jetty users often want to configure their web applications taking into account these different virtual hosts. Frequently, a machine with a single IP address will have different DNS resolvable names associated with it, and a webapp deployed on it must be reachable from all of the alternative names.
+
* 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.  
  
Other possibilities are to serve different web applications from different virtual hosts.
+
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:
  
==Configuration of virtual hosts==
+
* 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: [[Jetty/Tutorial/Embedding_Jetty|Embedding Jetty]].
 +
* Within an explicitly deployed webapp (no webapp provider) listed in [[Jetty/Reference/jetty.xml|jetty.xml]] or similar.
 +
* Using a WEB-INF/jetty-web.xml file (deprecated, but works with the webapp provider if you do not use the context provider).
 +
For descriptions of the various ways to configure Jetty, including links to documents that provide detailed configuration instructions, see [[Jetty/Howto/Configure_Jetty|How to Configure Jetty]].
  
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:
+
The examples that follow set virtual hosts in the preferred way, by calling the method ContextHandler.[http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/handler/ContextHandler.html#setVirtualHosts setVirtualHosts].
 +
 
 +
==Configuring Virtual Hosts==
 +
 
 +
When configuring a web application, you can supply a list of IP addresses and names at which the web application is reachable. Suppose you have a machine with these IP addresses and DNS resolvable names:
  
 
* 333.444.555.666
 
* 333.444.555.666
Line 23: Line 31:
 
* www.blah.org
 
* 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:
+
Suppose you have a webapp, ''xxx.war'', that you want all of the above names and addresses to serve. You would configure the webapp as follows:
 +
<source lang="xml">
 +
<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>
 +
</source>
  
<div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
Assuming you have configured a connector listening on port 8080, webapp ''xxx.war'' is available at all of the following addresses:
  
+
* <nowiki>http://333.444.555.666:8080/xxx</nowiki>
    <span class="code-tag"><Configure class=<span class="code-quote">"org.mortbay.jetty.webapp.WebAppContext"</span>></span>
+
* <nowiki>http://127.0.0.1:8080/xxx</nowiki>
      <span class="code-tag"><Set name=<span class="code-quote">"contextPath"</span>></span>/xxx<span class="code-tag"></Set></span>
+
* <nowiki>http://www.blah.com:8080/xxx</nowiki>
      <span class="code-tag"><Set name=<span class="code-quote">"war"</span>></span><span class="code-tag"><SystemProperty name=<span class="code-quote">"jetty.home"</span>/></span>/webapps/xxx.war<span class="code-tag"></Set></span>
+
* <nowiki>http://www.blah.net:8080/xxx</nowiki>
      <span class="code-tag"><Set name=<span class="code-quote">"virtualHosts"</span>></span>
+
* <nowiki>http://www.blah.org:8080/xxx</nowiki>
        <span class="code-tag"><Array type=<span class="code-quote">"java.lang.String"</span>></span>
+
          <span class="code-tag"><Item></span>333.444.555.666<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>127.0.0.1<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.blah.com<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.blah.net<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.blah.org<span class="code-tag"></Item></span>
+
        <span class="code-tag"></Array></span>
+
      <span class="code-tag"></Set></span>
+
    <span class="code-tag"></Configure></span>
+
  
</div></div>
+
==Configuring Different Webapps for Different Virtual Hosts==
  
Assuming we'd configured a connector listening on port 8080, then webapp '''xxx.war''' would be available at all of the following addresses:
+
You can configure different webapps for different virtual hosts by supplying a different list of virtual hosts for each webapp. For example, suppose your imaginary machine has these DNS names and IP 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
 
* 333.444.555.666
Line 66: Line 70:
 
* www.other.org
 
* 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:
+
Suppose also you 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:
  
<div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
<source lang="xml">
 +
<!-- 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>
 +
</source>
  
 
    <span class="code-tag"><span class="code-comment"><!-- webapp xxx.war --></span></span>
 
    <span class="code-tag"><Configure class=<span class="code-quote">"org.mortbay.jetty.webapp.WebAppContext"</span>></span>
 
      <span class="code-tag"><Set name=<span class="code-quote">"contextPath"</span>></span>/xxx<span class="code-tag"></Set></span>
 
      <span class="code-tag"><Set name=<span class="code-quote">"war"</span>></span><span class="code-tag"><SystemProperty name=<span class="code-quote">"jetty.home"</span>/></span>/webapps/xxx.war<span class="code-tag"></Set></span>
 
      <span class="code-tag"><Set name=<span class="code-quote">"virtualHosts"</span>></span>
 
        <span class="code-tag"><Array type=<span class="code-quote">"java.lang.String"</span>></span>
 
          <span class="code-tag"><Item></span>333.444.555.666<span class="code-tag"></Item></span>
 
          <span class="code-tag"><Item></span>127.0.0.1<span class="code-tag"></Item></span>
 
          <span class="code-tag"><Item></span>www.blah.com<span class="code-tag"></Item></span>
 
          <span class="code-tag"><Item></span>www.blah.net<span class="code-tag"></Item></span>
 
          <span class="code-tag"><Item></span>www.blah.org<span class="code-tag"></Item></span>
 
        <span class="code-tag"></Array></span>
 
      <span class="code-tag"></Set></span>
 
    <span class="code-tag"></Configure></span>
 
  
</div></div><div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
<source lang="xml">
 +
<!-- 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>
 +
</source>
  
+
Webapp ''xxx.war'' is still available at:
    <span class="code-tag"><span class="code-comment"><!-- webapp zzz.war --></span></span>
+
    <span class="code-tag"><Configure class=<span class="code-quote">"org.mortbay.jetty.webapp.WebAppContext"</span>></span>
+
      <span class="code-tag"><Set name=<span class="code-quote">"contextPath"</span>></span>/zzz<span class="code-tag"></Set></span>
+
      <span class="code-tag"><Set name=<span class="code-quote">"war"</span>></span><span class="code-tag"><SystemProperty name=<span class="code-quote">"jetty.home"</span>/></span>/webapps/zzz.war<span class="code-tag"></Set></span>
+
      <span class="code-tag"><Set name=<span class="code-quote">"virtualHosts"</span>></span>
+
        <span class="code-tag"><Array type=<span class="code-quote">"java.lang.String"</span>></span>
+
          <span class="code-tag"><Item></span>777.888.888.111<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.other.com<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.other.net<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.other.org<span class="code-tag"></Item></span>
+
        <span class="code-tag"></Array></span>
+
      <span class="code-tag"></Set></span>
+
    <span class="code-tag"></Configure></span>
+
  
</div></div>
+
* <nowiki>http://333.444.555.666:8080/xxx</nowiki>
 +
* <nowiki>http://127.0.0.1:8080/xxx</nowiki>
 +
* <nowiki>http://www.blah.com:8080/xxx</nowiki>
 +
* <nowiki>http://www.blah.net:8080/xxx</nowiki>
 +
* <nowiki>http://www.blah.org:8080/xxx</nowiki>
  
Webapp xxx.war is still available at:
+
But now webapp ''zzz.war'' is available at:
  
* http://333.444.555.666:8080/xxx
+
* <nowiki>http://777.888.888.111:8080/zzz</nowiki>
* http://127.0.0.1:8080/xxx
+
* <nowiki>http://www.other.com:8080/zzz</nowiki>
* http://www.blah.com:8080/xxx
+
* <nowiki>http://www.other.net:8080/zzz</nowiki>
* http://www.blah.net:8080/xxx
+
* <nowiki>http://www.other.org:8080/zzz</nowiki>
* http://www.blah.org:8080/xxx
+
  
But now webapp zzz.war is available at:
+
==Configuring Different Webapps for Different Virtual Hosts, But at the Same Context Path==
  
* http://777.888.888.111:8080/zzz
+
In the example above, webapp zzz.war is available not only at a certain set of virtual hosts, but also at the context path /zzz, whilst the other webapp is available at both a different set of virtual hosts, ''and'' at a different context path. What happens if you want them at the ''same'' context path, but still at different sets of virtual hosts? You just supply the ''same'' context path for each webapp, leaving the disjoint set of virtual host definitions as before:
* 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==
+
<source lang="xml">
 +
<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>
  
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?
+
</source>
  
Very simply, we just supply the '''same''' context path for each webapp, leaving the disjoint set of virtual host definitions as before:
+
<source lang="xml">
 +
<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>
 +
</source>
  
<div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
Now, webapp xxx.war is available at:
  
+
* <nowiki>http://333.444.555.666:8080/</nowiki>
    <span class="code-tag"><Configure class=<span class="code-quote">"org.mortbay.jetty.webapp.WebAppContext"</span>></span>
+
* <nowiki>http://127.0.0.1:8080/</nowiki>
      <span class="code-tag"><Set name=<span class="code-quote">"war"</span>></span><span class="code-tag"><SystemProperty name=<span class="code-quote">"jetty.home"</span>/></span>/webapps/xxx.war<span class="code-tag"></Set></span>
+
* <nowiki>http://www.blah.com:8080/</nowiki>
      <span class="code-tag"><Set name=<span class="code-quote">"contextPath"</span>></span>/<span class="code-tag"></Set></span>
+
* <nowiki>http://www.blah.net:8080/</nowiki>
      <span class="code-tag"><Set name=<span class="code-quote">"virtualHosts"</span>></span>
+
* <nowiki>http://www.blah.org:8080/</nowiki>
        <span class="code-tag"><Array type=<span class="code-quote">"java.lang.String"</span>></span>
+
          <span class="code-tag"><Item></span>333.444.555.666<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>127.0.0.1<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.blah.com<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.blah.net<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.blah.org<span class="code-tag"></Item></span>
+
        <span class="code-tag"></Array></span>
+
      <span class="code-tag"></Set></span>
+
    <span class="code-tag"></Configure></span>
+
  
</div></div><div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
and webapp zzz.war is available at:
  
+
* <nowiki>http://777.888.888.111:8080/</nowiki>
    <span class="code-tag"><Configure class=<span class="code-quote">"org.mortbay.jetty.webapp.WebAppContext"</span>></span>
+
* <nowiki>http://www.other.com:8080/</nowiki>
      <span class="code-tag"><Set name=<span class="code-quote">"war"</span>></span><span class="code-tag"><SystemProperty name=<span class="code-quote">"jetty.home"</span>/></span>/webapps/zzz.war<span class="code-tag"></Set></span>
+
* <nowiki>http://www.other.net:8080/</nowiki>
      <span class="code-tag"><Set name=<span class="code-quote">"contextPath"</span>></span>/<span class="code-tag"></Set></span>
+
* <nowiki>http://www.other.org:8080/</nowiki>
      <span class="code-tag"><Set name=<span class="code-quote">"virtualHosts"</span>></span>
+
        <span class="code-tag"><Array type=<span class="code-quote">"java.lang.String"</span>></span>
+
          <span class="code-tag"><Item></span>777.888.888.111<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.other.com<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.other.net<span class="code-tag"></Item></span>
+
          <span class="code-tag"><Item></span>www.other.org<span class="code-tag"></Item></span>
+
        <span class="code-tag"></Array></span>
+
      <span class="code-tag"></Set></span>
+
    <span class="code-tag"></Configure></span>
+
  
</div></div>
+
==Configuring Virtual Hosts with Non-ascii Characters==
  
Now, webapp xxx.war is available at:
+
[http://en.wikipedia.org/wiki/Internationalized_domain_name International domain names] are names containing non-ascii characters. For example <nowiki>"http://www.bücher.com"</nowiki>. The DNS internally remains based on ascii, so these kinds of names are translated via an encoding called [http://tools.ietf.org/html/rfc3492 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://333.444.555.666:8080/
+
<nowiki>http://www.åäö.com:8080/test/</nowiki>
* 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:
+
is translated to the following url:
 +
 
 +
<nowiki>http://www.xn--4cab6c.com:8080/test/</nowiki>
 +
 
 +
To use internationalized domain names with Jetty virtual hosts you need to supply the punycode form of the name in your context xml file (and of course you need to supply it to your DNS setup).
 +
 
 +
For example, if you are running a webapp on port 8080 at context /test, and you want to configure a virtual host for www.åäö.com, you configure its ascii equivalent in the context xml file for the context:
 +
 
 +
<source lang="xml">
 +
<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>
 +
</source>
 +
 
 +
After starting Jetty, you can enter the url <nowiki>http://www.åäö.com:8080/test/</nowiki> in a browser and reach the webapp.
  
* http://777.888.888.111:8080/
+
If you don't have any webapps deployed at /, hitting the URL <nowiki>http://www.åäö.com:8080</nowiki>reaches Jetty's default handler, which serves back a 404 page listing the available contexts:
* http://www.other.com:8080/
+
* http://www.other.net:8080/
+
* http://www.other.org:8080/
+
  
</div>Labels parameters    <div id="labels-section" class="pageSection"><div id="default-labels-header" class="section-header">
+
<source lang="bash">
 +
Error 404 - Not Found
  
==Labels==
+
No context on this server matched or handled this request.
  
</div><div class="labels-editor"><div id="labelsList"></div><span id="errorSpan" class="errorMessage error"></span><div id="labelInputSpan" class="labels-input"><div id="labelOperationErrorContainer" class="hidden"><span class="error"><span id="labelOperationErrorMessage" class="errorMessage"></span></span></div><div class="caption">Enter labels to add to this page:</div><div id="label-input-fields">  </div><div id="waitImageAndStatus">[[Image:wait.gif|Please wait]]  <span id="labelOperationStatus" class="smalltext"></span></div><div id="labelsAutocompleteList" class="aui-dd-parent"></div><div class="labels-tip"><div id="suggestedLabelsSpan"></div> Looking for a label? Just start typing. </div></div></div></div> <div id="comments-section" class="pageSection"><div class="section-header ">
+
Contexts known to this server are:
  
==Comments (1)==
+
/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
 +
</source>
  
* [/display/JETTY/Virtual+hosts?showComments=false#comments Hide Comments] [/display/JETTY/Virtual+hosts?showComments=true#comments Show Comments]
+
Notice that the link already has the punycode transformed domain name in it.
* [# Collapse All] <span id="collapse-wait" class="hidden">Collapsing…</span> <span id="expand-wait" class="hidden">Expanding…</span> [# Expand All]
+
  
</div>
 
  
# <div id="comment-32735320" class="comment  ">[/display/~mboehm [[Image:default.gif]]]Dec 01, 2007<div class="comment-header comment-toggle">====[/display/~mboehm Maximilian Böhm] says:====<span class="excerpt">Hi, i'm configuring jetty with some helper-methods. One problem is, that i have...</span></div><div class="comment-body"><div class="comment-content wiki-content">Hi,i'm configuring jetty with some helper-methods. One problem is, that i have to restart jetty to overtake the changes i've done with context.setVirtualHosts. Is there a mistake by me or doesn't it work else?Thanks in advanceGreetzPS.: Here the code:<div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">    <span class="code-keyword">public</span> void addVirtualHost(Context context, <span class="code-object">String</span> sDomain) <span class="code-keyword">throws</span> Exception{
 
      <span class="code-object">int</span> nOldVHosts = context.getVirtualHosts().length+1;
 
      <span class="code-object">String</span>[] nNewVHosts = <span class="code-keyword">new</span> <span class="code-object">String</span>[nOldVHosts];
 
      <span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; i < context.getVirtualHosts().length; i++) {
 
          nNewVHosts[i] = context.getVirtualHosts()[i];
 
      }
 
      nNewVHosts[nOldVHosts-1] = sDomain;
 
     
 
      context.setVirtualHosts(nNewVHosts);
 
     
 
      server.stop();
 
      server.start();
 
    }
 
   
 
</div>
 
| prereqs = (optional)
 
| steps = (optional)
 
| notes = (optional)
 
| examples = (optional)
 
| snippets = (optional) - for chunks of code that are too big to go into the Steps section, or screenshots
 
| more = (optional) - links, additional references
 
 
}}
 
}}

Latest revision as of 14:49, 23 April 2013



Introduction

Warning2.png
Jetty 7 and Jetty 8 are now EOL (End of Life)




THIS IS NOT THE DOCUMENTATION YOU ARE LOOKING FOR!!!!!






All development and stable releases are being performed with Jetty 9 and Jetty 10.






This wiki is now officially out of date and all content has been moved to the Jetty Documentation Hub






Direct Link to updated documentation: http://www.eclipse.org/jetty/documentation/current/configuring-virtual-hosts.html


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.

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 webapp provider) listed in jetty.xml or similar.
  • Using a WEB-INF/jetty-web.xml file (deprecated, but works with the webapp provider if you do not use the context provider).

For descriptions of the various ways to configure Jetty, including links to documents that provide detailed configuration instructions, see How to Configure Jetty.

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

Configuring Virtual Hosts

When configuring a web application, you can supply a list of IP addresses and names at which the web application is reachable. Suppose you 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 you have a webapp, xxx.war, that you want all of the above names and addresses to serve. You would configure the webapp as follows:

<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 you have configured a connector listening on port 8080, webapp xxx.war is 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

You can configure different webapps for different virtual hosts by supplying a different list of virtual hosts for each webapp. For example, suppose your 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 you 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 the example above, webapp zzz.war is available not only at a certain set of virtual hosts, but also at the context path /zzz, whilst the other webapp is available at both a different set of virtual hosts, and at a different context path. What happens if you want them at the same context path, but still at different sets of virtual hosts? You 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/

To use internationalized domain names with Jetty virtual hosts you need to supply the punycode form of the name in your context xml file (and of course you need to supply it to your DNS setup).

For example, if you are running a webapp on port 8080 at context /test, and you want to configure a virtual host for www.åäö.com, you 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, you can enter the url http://www.åäö.com:8080/test/ in a browser and reach the webapp.

If you don't have any webapps deployed at /, hitting the URL http://www.åäö.com:8080reaches Jetty's default handler, which serves back a 404 page listing the available contexts:

Error 404 - Not Found
 
No context on this server matched or handled this request.
 
Contexts known to this server are:
 
/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

Notice that the link already has the punycode transformed domain name in it.

Back to the top