原文地址:http://my.oschina.net/wangyongqing/blog/115647
Jetty用户经常想配置他们的web应用到不同的虚拟主机。通常情况下,一个单一的IP地址的机器有不同的DNS解析名与它相关联的,部署在这个机器上的web应用必须能够通过这些关联的DNS解析名访问到。Another possibility is to serve different web applications from different virtual hosts.
|
1
2
3
4
5
6
|
另一种可能是不同的虚拟主机为不同的web应用提供服务。你可以用不同的方法设置虚拟主机,包括:1)再context文件夹中放置一个context XML文件:setVirtualHosts. 这是一个完美的方法。2)用java调用内嵌式jetty服务3)再jetty.xml中明确列出要部署的项目列表或者类似的。 4)再项目的WEB-INF下面加一个自己的jetty-web.xml (在你不适用jetty提供的配置文件的情况下).对于不同的方式来配置虚拟主机,包括文件,提供详细的配置说明的链接的说明,请参阅如何配置 |
实例一:配置一个虚拟主机<!-- 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>如果你配置了jetty监听到8080端口,你可以通过如下方式访问到xxx.warhttp://333.444.555.666:8080/xxxhttp://127.0.0.1:8080/xxxhttp://www.blah.com:8080/xxxhttp://www.blah.net:8080/xxxhttp://www.blah.org:8080/xxx实例二:配置不用的虚拟主机用不同的contextPath<!-- 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><!-- lang: xml --><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>这里需要注意的是第二个没有配置127.0.0.1,因为两个都配置了就没法区分了应用xxx.war 通过下面能访问到:http://333.444.555.666:8080/xxxhttp://127.0.0.1:8080/xxxhttp://www.blah.com:8080/xxxhttp://www.blah.net:8080/xxxhttp://www.blah.org:8080/xxx应用 zzz.war 通过下面法师能访问到:http://777.888.888.111:8080/zzzhttp://www.other.com:8080/zzzhttp://www.other.net:8080/zzzhttp://www.other.org:8080/zzz实例三:配置不用的虚拟主机用相同的contextPath<!-- 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><!-- 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>应用 xxx.war 通过如下方式访问: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/应用 zzz.war 通过如下方式访问:http://777.888.888.111:8080/http://www.other.com:8080/http://www.other.net:8080/http://www.other.org:8080/原文请参考http://wiki.eclipse.org/Jetty/Howto/Configure_Virtual_Hosts |