blob: 4639d6276d151f4d43f5b32765257d4af87f846f [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Configuring Virtual Hosts</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty"><link rel="up" href="configuring-contexts.html" title="Chapter&nbsp;5.&nbsp;Configuring Contexts"><link rel="prev" href="configuring-contexts.html" title="Chapter&nbsp;5.&nbsp;Configuring Contexts"><link rel="next" href="ref-temporary-directories.html" title="Temporary Directories"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><link rel="stylesheet" href="css/highlighter/foundation.css"><script src="js/highlight.pack.js"></script><script>
hljs.initHighlightingOnLoad();
</script><link type="text/css" rel="stylesheet" href="css/font-awesome/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small">
Version: 9.3.28.v20191105</span></td><td style="width: 50%"></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Configuring Virtual Hosts</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="configuring-contexts.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;5.&nbsp;Configuring Contexts<br><a accesskey="p" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ref-temporary-directories.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at
<span class="website">www.webtide.com</span></a></h5><p>
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
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="configuring-virtual-hosts"></a>Configuring Virtual Hosts</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="section"><a href="configuring-virtual-hosts.html#configuring-a-virtual-host">Virtual Host Names</a></span></dt><dt><span class="section"><a href="configuring-virtual-hosts.html#_example_virtual_host_configuration">Example Virtual Host Configuration</a></span></dt><dt><span class="section"><a href="configuring-virtual-hosts.html#different-virtual-hosts-different-contexts">Using Different Sets of Virtual Hosts to Select Different Contexts</a></span></dt><dt><span class="section"><a href="configuring-virtual-hosts.html#different-virtual-hosts-different-context-same-path">Using Different Sets of Virtual Hosts to Select Different Contexts at the Same Context Path</a></span></dt></dl></div><p>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 <a class="link" href="configuring-virtual-hosts.html#different-virtual-hosts-different-contexts" title="Using Different Sets of Virtual Hosts to Select Different Contexts">different</a>, then a virtual host needs to be configured for each deployed <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/ContextHandler.html" target="_top">context</a> to indicate which names a context will respond to.</p><p>Virtual hosts are set on a <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/ContextHandler.html" target="_top">context</a> by calling the <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/ContextHandler.html#setVirtualHosts-java.lang.String:A-" target="_top"><code class="literal">setVirtualHosts</code></a> or <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/ContextHandler.html#addVirtualHosts-java.lang.String:A-" target="_top"><code class="literal">addVirtualHost</code></a> method which can be done in several ways:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">Using a <a class="link" href="configuring-specific-webapp-deployment.html#deployable-descriptor-file" title="Jetty Deployable Descriptor XML File">context XML</a> file in the webapps directory (see the example in <a class="link" href="/Users/joakim/Code/Jetty/jetty.project-9.3.x/jetty-documentation/../tests/test-webapps/test-jetty-webapp/src/main/config/demo-base/webapps/test.xml" target="_top">test.xml</a> in the Jetty distribution).</li><li class="listitem">Creating a <a class="link" href="deployment-architecture.html" title="Deployment Architecture">custom deployer</a> with a binding to configure virtual hosts for all contexts found in the same <code class="literal">webapps</code> directory.</li><li class="listitem">Calling the <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/ContextHandler.html#setVirtualHosts-java.lang.String:A-" target="_top">API</a> directly on an <a class="link" href="advanced-embedding.html" title="Chapter&nbsp;21.&nbsp;Embedding">embedded</a> usage.</li><li class="listitem">Using a <code class="literal">WEB-INF/jetty-web.xml</code> file (now deprecated).</li></ul></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="configuring-a-virtual-host"></a>Virtual Host Names</h3></div></div></div><p>Jetty supports the following styles of virtual host name:</p><div class="variablelist"><dl class="variablelist"><dt><span class="term">www.hostname.com</span></dt><dd>A fully qualified host name. It is important to list all variants as a site may receive traffic from both www.hostname.com and just hostname.com</dd><dt><span class="term">*.hostname.com</span></dt><dd>A wildcard qualified host which will match only one level of arbitrary names.
*.foo.com will match www.foo.com and m.foo.com, but not www.other.foo.com</dd><dt><span class="term">10.0.0.2</span></dt><dd>An IP address may be given as a virtual host name to indicate that a context should handle requests received on that server port that do not have a host name specified (HTTP/0.9 or HTTP/1.0).</dd><dt><span class="term">@ConnectorName</span></dt><dd>A connector name, which is not strictly a virtual host, but instead will only match requests that are received on connectors that have a matching name set with <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/AbstractConnector.html#setName(java.lang.String)" target="_top">Connector.setName(String)</a>.</dd><dt><span class="term">www.&#8730;integral.com</span></dt><dd>Non-ASCII and <a class="link" href="http://en.wikipedia.org/wiki/Internationalized_domain_name" target="_top">IDN</a> domain names can be set as virtual hosts using <a class="link" href="http://en.wikipedia.org/wiki/Punycode" target="_top">Puny Code</a> equivalents that may be obtained from a <a class="link" href="http://network-tools.com/idn-convert.asp" target="_top">Punycode/IDN converters</a>.
For example if the non-ASCII domain name <code class="literal">www.&#8730;integral.com</code> is given to a browser, then it will make a request that uses the domain name <code class="literal">www.xn--integral-7g7d.com</code>, which is the name that should be added as the virtual host name.</dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_example_virtual_host_configuration"></a>Example Virtual Host Configuration</h3></div></div></div><p>Virtual hosts can be used with any context that is a subclass of <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/ContextHandler.html" target="_top">ContextHandler</a>.
Lets look at an example where we configure a web application - represented by the <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/webapp/WebAppContext.html" target="_top">WebAppContext</a> class - with virtual hosts.
You supply a list of IP addresses and names at which the web application is reachable, such as the following:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">333.444.555.666</code></li><li class="listitem"><code class="literal">127.0.0.1</code></li><li class="listitem"><code class="literal">www.blah.com</code></li><li class="listitem"><code class="literal">www.blah.net</code></li><li class="listitem"><code class="literal">www.blah.org</code></li></ul></div><p>Suppose you have a webapp called <code class="literal">blah.war</code>, that you want all of the above names and addresses to be served at path "<code class="literal">/blah</code>".
Here&#8217;s how you would configure the virtual hosts with a <a class="link" href="configuring-specific-webapp-deployment.html#deployable-descriptor-file" title="Jetty Deployable Descriptor XML File">context XML</a> file:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"&gt;
&lt;Configure class="org.eclipse.jetty.webapp.WebAppContext"&gt;
&lt;Set name="contextPath"&gt;/blah&lt;/Set&gt;
&lt;Set name="war"&gt;&lt;Property name="jetty.webapps"/&gt;/webapps/blah.war&lt;/Set&gt;
&lt;Set name="virtualHosts"&gt;
&lt;Array type="java.lang.String"&gt;
&lt;Item&gt;333.444.555.666&lt;/Item&gt;
&lt;Item&gt;127.0.0.1&lt;/Item&gt;
&lt;Item&gt;www.blah.com&lt;/Item&gt;
&lt;Item&gt;www.blah.net&lt;/Item&gt;
&lt;Item&gt;www.blah.org&lt;/Item&gt;
&lt;/Array&gt;
&lt;/Set&gt;
&lt;/Configure&gt;</code></pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="different-virtual-hosts-different-contexts"></a>Using Different Sets of Virtual Hosts to Select Different Contexts</h3></div></div></div><p>You can configure different contexts to respond on different virtual hosts by supplying a specific list of virtual hosts for each one.</p><p>For example, suppose your imaginary machine has these DNS names:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">www.blah.com</code></li><li class="listitem"><code class="literal">www.blah.net</code></li><li class="listitem"><code class="literal">www.blah.org</code></li><li class="listitem"><code class="literal">www.other.com</code></li><li class="listitem"><code class="literal">www.other.net</code></li><li class="listitem"><code class="literal">www.other.org</code></li></ul></div><p>Suppose also you have 2 webapps, one called <code class="literal">blah.war</code> that you would like served from the <code class="literal">*.blah.*</code> names, and one called <code class="literal">other.war</code> that you want served only from the <code class="literal">*.other.*</code> names.</p><p>Using the method of preparing <a class="link" href="configuring-specific-webapp-deployment.html#deployable-descriptor-file" title="Jetty Deployable Descriptor XML File">contextXML</a> files, one for each webapp yields the following:</p><p>For <code class="literal">blah</code> webapp:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"&gt;
&lt;Configure class="org.eclipse.jetty.webapp.WebAppContext"&gt;
&lt;Set name="contextPath"&gt;/blah&lt;/Set&gt;
&lt;Set name="war"&gt;&lt;Property name="jetty.webapps"/&gt;/webapps/blah.war&lt;/Set&gt;
&lt;Set name="virtualHosts"&gt;
&lt;Array type="java.lang.String"&gt;
&lt;Item&gt;www.blah.com&lt;/Item&gt;
&lt;Item&gt;www.blah.net&lt;/Item&gt;
&lt;Item&gt;www.blah.org&lt;/Item&gt;
&lt;/Array&gt;
&lt;/Set&gt;
&lt;/Configure&gt;</code></pre><p>These URLs now resolve to the blah context (ie <code class="literal">blah.war</code>):</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">http://www.blah.com/blah</code></li><li class="listitem"><code class="literal">http://www.blah.net/blah</code></li><li class="listitem"><code class="literal">http://www.blah.org/blah</code></li></ul></div><p>For <code class="literal">other</code> webapp:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"&gt;
&lt;Configure class="org.eclipse.jetty.webapp.WebAppContext"&gt;
&lt;Set name="contextPath"&gt;/other&lt;/Set&gt;
&lt;Set name="war"&gt;&lt;Property name="jetty.webapps"/&gt;/webapps/other.war&lt;/Set&gt;
&lt;Set name="virtualHosts"&gt;
&lt;Array type="java.lang.String"&gt;
&lt;Item&gt;www.other.com&lt;/Item&gt;
&lt;Item&gt;www.other.net&lt;/Item&gt;
&lt;Item&gt;www.other.org&lt;/Item&gt;
&lt;/Array&gt;
&lt;/Set&gt;
&lt;/Configure&gt;</code></pre><p>These URLs now resolve to the other context (i.e. <code class="literal">other.war</code>):</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">http://www.other.com/other</code></li><li class="listitem"><code class="literal">http://www.other.net/other</code></li><li class="listitem"><code class="literal">http://www.other.org/other</code></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="different-virtual-hosts-different-context-same-path"></a>Using Different Sets of Virtual Hosts to Select Different Contexts at the Same Context Path</h3></div></div></div><p>In the previous section we setup 2 different contexts to be served from different virtual hosts at <span class="emphasis"><em>different</em></span> 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.</p><p>Consider an example where we have the same set of DNS names as before, and the same webapps <code class="literal">blah.war</code> and <code class="literal">other.war</code>. We still want <code class="literal">blah.war</code> to be served in response to hostnames of <code class="literal">*.blah.*</code>, and we still want <code class="literal">other.war</code> to be served in response to <code class="literal">*.other.*</code> names.
However, we would like<span class="emphasis"><em>all</em></span> of our clients to use the <code class="literal">"/"</code> context path, no matter which context is being targeted.</p><p>In other words, we want all of the following URLs to map to <code class="literal">blah.war</code>:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">http://www.blah.com/</code></li><li class="listitem"><code class="literal">http://www.blah.net/</code></li><li class="listitem"><code class="literal">http://www.blah.org/</code></li></ul></div><p>Similarly, we want the following URLs to map to <code class="literal">other.war</code>:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">http://www.other.com/</code></li><li class="listitem"><code class="literal">http://www.other.net/</code></li><li class="listitem"><code class="literal">http://www.other.org/</code></li></ul></div><p>To achieve this, we simply use the same context path of <code class="literal">/</code> for each of our webapps, while still applying our different set of virtual host names.</p><p>For foo webapp:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"&gt;
&lt;Configure class="org.eclipse.jetty.webapp.WebAppContext"&gt;
&lt;Set name="contextPath"&gt;/&lt;/Set&gt;
&lt;Set name="war"&gt;&lt;Property name="jetty.webapps"/&gt;/webapps/foo.war&lt;/Set&gt;
&lt;Set name="virtualHosts"&gt;
&lt;Array type="java.lang.String"&gt;
&lt;Item&gt;www.blah.com&lt;/Item&gt;
&lt;Item&gt;www.blah.net&lt;/Item&gt;
&lt;Item&gt;www.blah.org&lt;/Item&gt;
&lt;/Array&gt;
&lt;/Set&gt;
&lt;/Configure&gt;</code></pre><p>For bar webapp:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"&gt;
&lt;Configure class="org.eclipse.jetty.webapp.WebAppContext"&gt;
&lt;Set name="contextPath"&gt;/&lt;/Set&gt;
&lt;Set name="war"&gt;&lt;Property name="jetty.webapps"/&gt;/webapps/bar.war&lt;/Set&gt;
&lt;Set name="virtualHosts"&gt;
&lt;Array type="java.lang.String"&gt;
&lt;Item&gt;www.other.com&lt;/Item&gt;
&lt;Item&gt;www.other.net&lt;/Item&gt;
&lt;Item&gt;www.other.org&lt;/Item&gt;
&lt;/Array&gt;
&lt;/Set&gt;
&lt;/Configure&gt;</code></pre></div></div><script type="text/javascript">
SyntaxHighlighter.all()
</script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="configuring-contexts.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="configuring-contexts.html"><i class="fa fa-chevron-up" aria-hidden="true"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ref-temporary-directories.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;5.&nbsp;Configuring Contexts&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;Temporary Directories</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout">
See an error or something missing?
<span class="callout"><a href="http://github.com/eclipse/jetty.project">Contribute to this documentation at
<span class="website"><i class="fa fa-github" aria-hidden="true"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2019-11-05)</i></span></div></p></body></html>