---
title: "一个jetty部署多个项目配置之方法"
author: "songzhan"
date: "2014-07-22T22:43:35Z"
tags: []
categories: ["企业架构"]
slug: "jetty"
draft: false
lastmod: "2014-07-22T22:43:35Z"
id: 2095143
url: "https://songzhan.iteye.com/blog/2095143"
---

原文地址：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.

[?](http://my.oschina.net/wangyongqing/blog/115647)

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提供的配置文件的情况下).`

`对于不同的方式来配置虚拟主机，包括文件，提供详细的配置说明的链接的说明，请参阅如何配置`

[jetty](http://wiki.eclipse.org/Jetty/Howto/Configure_Jetty)

[?](http://my.oschina.net/wangyongqing/blog/115647)

 

`实例一：配置一个虚拟主机`

 

`<!-- 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.war`

`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`

 

`实例二：配置不用的虚拟主机用不同的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/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`

`应用 zzz.war 通过下面法师能访问到:`

`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`

 

`实例三：配置不用的虚拟主机用相同的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`