Advertise with us

Our eBooks

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) : 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:


 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 name="myvirtualhost" alias="my-wildfly" default-web-module="welcome.war" >
            <access-log prefix="myvirtualhost"/>

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:


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 AS 7 Virtual Host configuration

Here's the CLI script you need to launch:


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"/>

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:


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:

   <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"
                transactionManagerObjectName="jboss:service=TransactionManager" ></Valve>

         <Host name="myvirtualhost">
            <Valve className="org.jboss.web.tomcat.service.jca.CachedConnectionValve"
                transactionManagerObjectName="jboss:service=TransactionManager" ></Valve>

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

© 2021 All Rights Reserved.

Please publish modules in offcanvas position.