![]() Version: 9.3.9.v20160517 |
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services for sponsored feature development
A virtual host is an alternative name, registered in DNS, for an IP address such that multiple domain names will resolve to the same IP of a shared server instance. If the content to be served to the aliases names is different, then a virtual host needs to be configured for each deployed context to indicate which names a context will respond to.
Virtual hosts are set on a
context
by calling the
setVirtualHosts
or
addVirtualHost
method which can be done either
WEB-INF/jetty-web.xml file (deprecated).Jetty supports the following styles of virtual host name:
www.√integral.com is given to a
browser, then it will make a request that uses the domain name
www.xn--integral-7g7d.com, which is the name that should be added as
the virtual host name.Virtual hosts can be used with any context that is a subclass of ContextHandler. Lets look at an example where we configure a web application - represented by the WebAppContext class - with virtual hosts. You 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 these DNS resolvable names:
333.444.555.666127.0.0.1www.blah.comwww.blah.netwww.blah.orgSuppose you have a webapp called blah.war, that you want all of the
above names and addresses to be served at path "/blah". Here’s how you
would configure the virtual hosts with a
context XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/blah</Set>
<Set name="war"><Property name="jetty.webapps"/>/webapps/blah.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>You can configure different contexts to respond on different virtual hosts by supplying a specific list of virtual hosts for each one.
For example, suppose your imaginary machine has these DNS names:
www.blah.comwww.blah.netwww.blah.orgwww.other.comwww.other.netSuppose also you have 2 webapps, one called blah.war that you would
like served from the *.blah.* names, and one called other.war that
you want served only from the *.other.* names.
Using the method of preparing context XML files, one for each webapp yields the following:
For blah webapp:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/blah</Set>
<Set name="war"><Property name="jetty.webapps"/>/webapps/blah.war</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>www.blah.com</Item>
<Item>www.blah.net</Item>
<Item>www.blah.org</Item>
</Array>
</Set>
</Configure>These urls now resolve to the blah context (ie blah.war):
http://www.blah.com/blahhttp://www.blah.net/blahhttp://www.blah.org/blahFor other webapp:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/other</Set>
<Set name="war"><Property name="jetty.webapps"/>/webapps/other.war</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>www.other.com</Item>
<Item>www.other.net</Item>
<Item>www.other.org</Item>
</Array>
</Set>
</Configure>These urls now resolve to the other context (ie other.war):
http://www.other.com/otherhttp://www.other.net/otherhttp://www.other.org/otherIn the previous section we setup 2 different contexts to be served from different virtual hosts at different context paths. However, there is no requirement that the context paths must be distinct: you may use the same context path for multiple contexts, and use virtual hosts to determine which one is served for a given request.
Consider an example where we have the same set of DNS names as before,
and the same webapps blah.war and other.war. We still want
blah.war to be served in response to hostnames of *.blah.*, and we
still want other.war to be served in response to *.other.* names.
However, we would likeall of our clients to use the "/" context
path, no matter which context is being targeted.
In other words, we want all of the following urls to map to blah.war:
http://www.blah.com/http://www.blah.net/http://www.blah.org/Similarly, we want the following urls to map to other.war:
http://www.other.com/http://www.other.net/http://www.other.org/To achieve this, we simply use the same context path of "/" for each of our webapps, whilst still applying our different set of virtual host names.
For foo webapp:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="war"><Property name="jetty.webapps"/>/webapps/foo.war</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>www.blah.com</Item>
<Item>www.blah.net</Item>
<Item>www.blah.org</Item>
</Array>
</Set>
</Configure>For bar webapp:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="war"><Property name="jetty.webapps"/>/webapps/bar.war</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>www.other.com</Item>
<Item>www.other.net</Item>
<Item>www.other.org</Item>
</Array>
</Set>
</Configure>