Jetty virtual hosts
From Oxxus Wiki
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).
Contents |
Using setVirtaulHosts in context XML
When configuring a web application, you can supply a list of IP addresses and names at which the web application will be reachable. Example below assumes a 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
Assuming there is a webapp, xxx.war, and that it needs to be served from all of the above names and addresses, then the webapp configuration would be like below:
<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>
Configuring different webapps for different virtual hosts
This is accomplished simply by supplying a different list of virtual hosts for each webapp. Example below supposes our a machine with 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
Another assumption is that there is another webapp, zzz.war, where xxx.war is 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 we want them at the same context path, but still at different sets of virtual hosts?
This is achieved simply by supplying 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/