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
Line 41: Line 41:
 
Assuming we'd configured a connector listening on port 8080, then webapp '''xxx.war''' would be available at all of the following addresses:
 
Assuming we'd 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
+
* <nowiki>http://333.444.555.666:8080/xxx</nowiki>
* http://127.0.0.1:8080/xxx
+
* <nowiki>http://127.0.0.1:8080/xxx</nowiki>
* http://www.blah.com:8080/xxx
+
* <nowiki>http://www.blah.com:8080/xxx</nowiki>
* http://www.blah.net:8080/xxx
+
* <nowiki>http://www.blah.net:8080/xxx</nowiki>
* http://www.blah.org:8080/xxx
+
* <nowiki>http://www.blah.org:8080/xxx</nowiki>
  
 
==Configuring different webapps for different virtual hosts==
 
==Configuring different webapps for different virtual hosts==
Line 64: Line 64:
  
 
<source lang="xml">
 
<source lang="xml">
 
+
<!-- webapp xxx.war -->
+
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <!-- webapp xxx.war -->
+
  <Set name="contextPath">/xxx</Set>
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
+
  <Set name="war"><SystemProperty name="jetty.home"/>/webapps/xxx.war</Set>
      <Set name="contextPath">/xxx</Set>
+
  <Set name="virtualHosts">
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/xxx.war</Set>
+
    <Array type="java.lang.String">
      <Set name="virtualHosts">
+
      <Item>333.444.555.666</Item>
        <Array type="java.lang.String">
+
      <Item>127.0.0.1</Item>
          <Item>333.444.555.666</Item>
+
      <Item>www.blah.com</Item>
          <Item>127.0.0.1</Item>
+
      <Item>www.blah.net</Item>
          <Item>www.blah.com</Item>
+
      <Item>www.blah.org</Item>
          <Item>www.blah.net</Item>
+
    </Array>
          <Item>www.blah.org</Item>
+
  </Set>
        </Array>
+
</Configure>
      </Set>
+
</source>
    </Configure>
+
    </source>
+
  
  
    <source lang="xml">
+
<source lang="xml">
    <!-- webapp zzz.war -->
+
<!-- webapp zzz.war -->
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
+
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="contextPath">/zzz</Set>
+
  <Set name="contextPath">/zzz</Set>
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/zzz.war</Set>
+
  <Set name="war"><SystemProperty name="jetty.home"/>/webapps/zzz.war</Set>
      <Set name="virtualHosts">
+
  <Set name="virtualHosts">
        <Array type="java.lang.String">
+
    <Array type="java.lang.String">
          <Item>777.888.888.111</Item>
+
      <Item>777.888.888.111</Item>
          <Item>www.other.com</Item>
+
      <Item>www.other.com</Item>
          <Item>www.other.net</Item>
+
      <Item>www.other.net</Item>
          <Item>www.other.org</Item>
+
      <Item>www.other.org</Item>
        </Array>
+
    </Array>
      </Set>
+
  </Set>
    </Configure>
+
</Configure>
 
+
 
</source>
 
</source>
  
 
Webapp xxx.war is still available at:
 
Webapp xxx.war is still available at:
  
* http://333.444.555.666:8080/xxx
+
* <nowiki>http://333.444.555.666:8080/xxx</nowiki>
* http://127.0.0.1:8080/xxx
+
* <nowiki>http://127.0.0.1:8080/xxx</nowiki>
* http://www.blah.com:8080/xxx
+
* <nowiki>http://www.blah.com:8080/xxx</nowiki>
* http://www.blah.net:8080/xxx
+
* <nowiki>http://www.blah.net:8080/xxx</nowiki>
* http://www.blah.org:8080/xxx
+
* <nowiki>http://www.blah.org:8080/xxx</nowiki>
  
 
But now webapp zzz.war is available at:
 
But now webapp zzz.war is available at:
  
* http://777.888.888.111:8080/zzz
+
* <nowiki>http://777.888.888.111:8080/zzz</nowiki>
* http://www.other.com:8080/zzz
+
* <nowiki>http://www.other.com:8080/zzz</nowiki>
* http://www.other.net:8080/zzz
+
* <nowiki>http://www.other.net:8080/zzz</nowiki>
* http://www.other.org:8080/zzz
+
* <nowiki>http://www.other.org:8080/zzz</nowiki>
  
 
==Configuring different webapps for different virtual hosts, but at the same context path==
 
==Configuring different webapps for different virtual hosts, but at the same context path==
Line 122: Line 119:
  
 
<source lang="xml">
 
<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>
  
+
</source>
    <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>
+
 
+
</source><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 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>
 
</source>
  
 
Now, webapp xxx.war is available at:
 
Now, webapp xxx.war is available at:
  
* http://333.444.555.666:8080/
+
* <nowiki>http://333.444.555.666:8080/</nowiki>
* http://127.0.0.1:8080/
+
* <nowiki>http://127.0.0.1:8080/</nowiki>
* http://www.blah.com:8080/
+
* <nowiki>http://www.blah.com:8080/</nowiki>
* http://www.blah.net:8080/
+
* <nowiki>http://www.blah.net:8080/</nowiki>
* http://www.blah.org:8080/
+
* <nowiki>http://www.blah.org:8080/</nowiki>
  
 
and webapp zzz.war is available at:
 
and webapp zzz.war is available at:
  
* http://777.888.888.111:8080/
+
* <nowiki>http://777.888.888.111:8080/</nowiki>
* http://www.other.com:8080/
+
* <nowiki>http://www.other.com:8080/</nowiki>
* http://www.other.net:8080/
+
* <nowiki>http://www.other.net:8080/</nowiki>
* http://www.other.org:8080/
+
* <nowiki>http://www.other.org:8080/</nowiki>
  
  
 
}}
 
}}

Revision as of 17:43, 12 July 2010



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".

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.

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.

Other possibilities are to serve different web applications from different virtual hosts.

Let's examine these possibilities.

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'd 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/

Back to the top