A virtual host is an internet domain name, registered in the Domain Name Server (DNS), for an IP address such that multiple virtual hosts will resolve to the same IP address of a single server instance.
If you have multiple web applications deployed on the same Jetty server, by using virtual hosts you will be able to target a specific web application.
For example, you may have a web application for your business and a web application for your hobbies , both deployed in the same Jetty server.
By using virtual hosts, you will be able to have the first web application available at http://domain.biz/
, and the second web application available at http://hobby.net/
.
Another typical case is when you want to use different subdomains for different web application, for example a project website is at http://project.org/
and the project documentation is at http://docs.project.org
.
Virtual hosts can be used with any context that is a subclass of ContextHandler.
Jetty supports the following variants to be specified as virtual host names:
www.hostname.com
-
A fully qualified domain name. It is important to list all variants as a site may receive traffic for both
www.hostname.com
andhostname.com
. *.hostname.com
-
A wildcard domain name which will match only one level of arbitrary subdomains. *.foo.com will match www.foo.com and m.foo.com, but not www.other.foo.com.
10.0.0.2
-
An IP address may be set as a virtual host to indicate that a web application should handle requests received on the network interface with that IP address for protocols that do not indicate a host name such as HTTP/0.9 or HTTP/1.0.
@ConnectorName
-
A Jetty server
Connector
name to indicate that a web application should handle requests received on the serverConnector
with that name, and therefore received on a specific socket address (either an IP port forServerConnector
, or a Unix-Domain path forUnixDomainServerConnector
). A serverConnector
name can be set via {javadoc-url}/org/eclipse/jetty/server/AbstractConnector.html#setName(java.lang.String). www.√integral.com
-
Non-ASCII and IDN domain names can be set as virtual hosts using Puny Code equivalents that may be obtained from a Punycode/IDN converters. For example if the non-ASCII domain name
www.√integral.com
is given to a browser, then the browser will make a request that uses the domain namewww.xn—integral-7g7d.com
, which is the name that should be added as the virtual host name.
If you have a web application mywebapp.war
you can configure its virtual hosts in this way:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/mywebapp</Set>
<Set name="war">/opt/webapps/mywebapp.war</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>mywebapp.com</Item>
<Item>www.mywebapp.com</Item>
<Item>mywebapp.net</Item>
<Item>www.mywebapp.net</Item>
</Array>
</Set>
</Configure>
Your web application will be available at:
-
http://mywebapp.com/mywebapp
-
http://www.mywebapp.com/mywebapp
-
http://mywebapp.net/mywebapp
-
http://www.mywebapp.net/mywebapp
Note
|
You configured the As such, a request to Likewise, a request to |
If you want to deploy different web applications to the same context path, typically the root context path /
, you must use virtual hosts to differentiate among web applications.
You have domain.war
that you want to deploy at http://domain.biz/
and hobby.war
that you want to deploy at http://hobby.net
.
To achieve this, you simply use the same context path of /
for each of your webapps, while specifying different virtual hosts for each of your webapps:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="war">/opt/webapps/domain.war</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>domain.biz</Item>
</Array>
</Set>
</Configure>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="war">/opt/webapps/hobby.war</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>hobby.net</Item>
</Array>
</Set>
</Configure>
Sometimes it is required to serve different web applications from different socket addresses (either different IP ports, or different Unix-Domain paths), and therefore from different server Connector
s.
For example, you want requests to http://localhost:8080/
to be served by one web application, but requests to http://localhost:9090/
to be served by another web application.
This configuration may be useful when Jetty sits behind a load balancer.
In this case, you want to configure multiple connectors, each with a different name, and then reference the connector name in the web application virtual host configuration:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="war">/opt/webapps/domain.war</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>@port8080</Item>
</Array>
</Set>
</Configure>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="war">/opt/webapps/hobby.war</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>@port9090</Item>
</Array>
</Set>
</Configure>
Note
|
Web application Likewise, web application See this section for further information about how to configure connectors. |