WildFly 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 my-wildfly

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 “welcome.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 it requires just two steps:

1) Define a new Host in your Undertow configuration that will be used to handle the Virtual Hosting

2) If your applications aren’t deployed as default Web application, specify the Virtual Host in the jboss-web.xml file.

So, we start by defining a new Host named “virtualhost” with the alias “my-wildfly” from the CLI:

batch
/subsystem=undertow/server=default-server/host=myvirtualhost:add(alias=["my-wildfly"])
/subsystem=undertow/server=default-server/host=myvirtualhost/setting=access-log:add(prefix="myvirtualhost")
/subsystem=undertow/server=default-server/host=myvirtualhost:write-attribute(name=default-web-module,value=welcome.war)
run-batch

This will results in the following change in your configuration:

<subsystem xmlns="urn:jboss:domain:undertow:3.1">
    <buffer-cache name="default"/>
    <server name="default-server">
        <http-listener name="default" socket-binding="http"/>
        <host name="default-host" alias="localhost">
            <location name="/" handler="welcome-content"/>
            <filter-ref name="server-header"/>
            <filter-ref name="x-powered-by-header"/>
            <access-log prefix="default"/>
        </host>
        <host name="myvirtualhost" alias="my-wildfly" default-web-module="welcome.war" >
            <access-log prefix="myvirtualhost"/>
        </host>
    </server>

Realod your configuration. Now every request for http://localhost:8080/ will land on the default-host host. Howevert, if you request http://my-wildfly:8080/, the request will be handled by the secondary host we have added.

If you want that an application can only be accessed through one specific virtual host, you have to specify it into the jboss-web.xml deployment descriptor. In this example we will also deploy the Web application “test” that Virtual Host:

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

Defining the Virtual Host through the Admin Console

Defining a new Host can also be 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

JBoss 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>
Found the article helpful? if so please follow us on Socials