JBoss AS Virtual Host configuration

Virtual hosting is a mechanism whereby one web server process can serve multiple domain names, giving each domain the appearance of having its own server. In this tutorial we will show how to create and use a virtual host address for a JBoss web application.

Name-based virtual hosting is created on any web server by establishing an aliased IP address in the Domain Name Service (DNS) data and telling the web server to map all requests destined for the aliased address to a particular directory of web pages. For demonstration purposes, I'll use a static hosts file, since that's the easiest way to set up aliases for testing purposes.

To use virtual hosts you just need to set up the DNS or hosts data for the host. For testing, making an IP alias for localhost is sufficient. The first thing we need to do is setting up the Virtual Host alias into the host file (c:\windows\system32\drivers\etc\hosts for Windows or /etc/hosts for Linux) :

127.0.0.1 home

Now the application server. We need to do is adding a virtual host entry to your JBoss AS configuration file. Suppose we want to add a virtual host alias "home" which will be referenced in the configuration with the name "myvirtualhost". The default web module for this virtual host will be an application named "example.war". Let's see how to do in in all JBoss AS flavours

WildFly Virtual Host configuration

Configuring a Virtual Host with WildFly is pretty simple and can be fully done through the Web administration console. From the Configuration upper tab expand Web | HTTP| Default Server. Select Hosts and click on the Add button:

wildfly virtual host configuration 

In the following window enter your VirtualHost Name, Alias and (optionally) the default Web application:

wildfly virtual host configuration

Last, we need to include the Virtual Host name in the jboss-web.xml deployment descriptor. In case the application is going to be deployed on the root Web context, we will add this information in the context-root block:

<jboss-web>
     <context-root>/</context-root>
     <virtual-host>myvirtualhost</virtual-host>
</jboss-web>

JBoss AS 7 Virtual Host configuration

Here's the CLI script you need to launch:

/subsystem=web/virtual-server=myvirtualhost/:add(default-web-module=example.war,alias=["home"])					

Alternatively you can enter manually the following configuration in your standalone.xml/domain.xml file:

<virtual-server name="myvirtualhost" default-web-module="example.war">
       <alias name="home"/>
</virtual-server>

Now we need to apply a little change in our example.war Web application: we will add the jboss-web.xml deployment descriptor containing a reference to the virtual host we have just created:

<jboss-web>
     <context-root>/</context-root>
     <virtual-host>myvirtualhost</virtual-host>
</jboss-web>

That's all. Now your web application will be the default web application will be accessible with http://home:8080/

JBoss AS 4-5-6 Virtual Host configuration

The main difference here is that we will use the embedded Tomcat server.xml file to configure the Virtual Host entry:

<Server>
   <Service name="jboss.web">

      <!-- A HTTP/1.1 Connector on port 8080 -->
      <Connector protocol="HTTP/1.1" port="8080" address="${jboss.bind.address}" 
               connectionTimeout="20000" redirectPort="8443" ></Connector>

      <Engine name="jboss.web" defaultHost="localhost">
...
         <Host name="localhost"> 
...
            <Valve className="org.jboss.web.tomcat.service.jca.CachedConnectionValve"
                cachedConnectionManagerObjectName="jboss.jca:service=CachedConnectionManager"
                transactionManagerObjectName="jboss:service=TransactionManager" ></Valve>
         </Host>

         <Host name="myvirtualhost">
            <Alias>myvirtualhost</Alias>
            <Valve className="org.jboss.web.tomcat.service.jca.CachedConnectionValve"
                cachedConnectionManagerObjectName="jboss.jca:service=CachedConnectionManager"
                transactionManagerObjectName="jboss:service=TransactionManager" ></Valve>
         </Host>
    </Engine>
  </Service>
</Server>

The jboss-web.xml deployment descriptor just stays the same:

<jboss-web>
     <context-root>/</context-root>
     <virtual-host>myvirtualhost</virtual-host>
</jboss-web>

Related articles available on mastertheboss.com

How do I reload dynamically my jsp from an ear ?

  Deploy your Jsp as part of an application deployed in exploded

Where is my compiled JSP ?

JBoss recipe of the day

How do you configure JBoss to enable HTTP logging ?

To enable HTTP logging, you need to go to the deploy/jbossweb-tom

JBoss Struts tutorial

This article explains how you can setup quickly an application ba

How to restrict access to JBoss web application by IP or Host?

You can create easily Access control list based on the IP address

How do you configure a Web Application as Default Web app ?

  JBoss AS 4-5-6 First you have to remove the default

Follow us on Twitter