How to run multiple HTTP Ports in JBoss / Wildfly

In this short tutorial we will learn a simple configuration skill to start multiple HTTP Ports on WildFly application server and how to restrict each port to a specific Web application deployed on it.

HTTP Ports are managed by undertow in the server element which includes http/https/ajp listeners which are configured on a server. In order to add another http port, perform the following steps:

First, add another http listener in your server near the “default” listener: (the same technique can be used for an https/ajp listener):

 <!-- default http-listener -->
<http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
 <!-- additional http listener -->
<http-listener name="http-extra" socket-binding="http-extra" redirect-socket="https" enable-http2="true"/>

Next, in the socket-binding section, add the http-extra socket binding to that port will be opened for that listener:

  <socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
        . . . . .
        <socket-binding name="http-extra" port="${jboss.http.port:8090}"/>

Finally, start WildFly and check that both port 8080 and 8090 are listening:

$ netstat -an | grep -E '8080|8090'
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:8090          0.0.0.0:*               LISTEN   

How to assign each HTTP Port to a Web application

With the current configuration, you are exposing all Web applications on two HTTP Ports. A real world use case would be to assign each Port to a single application.

For example, if you are running SOAP Web Services, you could run your Web services on a different Port. See this tutorial to learn more about this topic: How to change the default Web Service deployment Port ?

To assign a different port for each Web application we need to define an Undertow filter which denies access to one or more application matching a path-prefix.

Here is how to create two expression filters:

  • webapp1-port-filter: to allow the Web application “webapp1” on Port 8080
  • webapp2-port-filter: to allow the Web application “webapp2” on Port 8090

Here is the CLI command to create the filter:

/subsystem=undertow/configuration=filter/expression-filter=webapp1-port-filter:add(expression="path-prefix('/webapp1') and not equals(%p, 8080) -> response-code(404)")
/subsystem=undertow/server=default-server/host=default-host/filter-ref=myapp1-port-filter:add

/subsystem=undertow/configuration=filter/expression-filter=webapp2-port-filter:add(expression="path-prefix('/webapp2') and not equals(%p, 8090) -> response-code(404)")
/subsystem=undertow/server=default-server/host=default-host/filter-ref=myapp2-port-filter:add

Here is the resulting undertow configuration:

<subsystem xmlns="urn:jboss:domain:undertow:12.0" default-server="default-server" default-virtual-host="default-host" default-servlet-container="default" default-security-domain="other" statistics-enabled="${wildfly.undertow.statistics-enabled:${wildfly.statistics-enabled:false}}">
    <buffer-cache name="default"/>
    <server name="default-server">
        <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
        <http-listener name="http-extra" socket-binding="http-extra" redirect-socket="https" enable-http2="true"/>
        <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true"/>
        <host name="default-host" alias="localhost">
            <location name="/" handler="welcome-content"/>
            <filter-ref name="webapp1-port-filter"/>
            <filter-ref name="webapp2-port-filter"/>
            <http-invoker security-realm="ApplicationRealm"/>
        </host>
    </server>
    <servlet-container name="default">
        <jsp-config/>
        <websockets/>
    </servlet-container>
    <handlers>
        <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
    </handlers>
    <filters>
        <expression-filter name="webapp1-port-filter" expression="path-prefix('/webapp1') and not equals(%p, 8080) -> response-code(404)"/>
        <expression-filter name="webapp2-port-filter" expression="path-prefix('/webapp2') and not equals(%p, 8090) -> response-code(404)"/>
    </filters>
</subsystem>

Testing the undertow filter

As proof of concept, we will deploy two web applications named “webapp1.war” and “webapp2.war” on WildFly.

As expected, webapp1 will be available on Port 8080:

On the other hand, webapp2 won’t be available on Port 8090:

Conversely, webapp2 will be available on port 8090:

Finally, webapp2 won’t be available on the default port 8080:

It’s worth mentioning that in our filter we have set as response code 404 (“Not found”) when the filter does not match with the expression. Depending on your policy, you might want to use instead code 403 (“Forbidden”) if the filter match fails.

Conclusion

We have learnt how to enable multiple HTTP Ports on your WildFly / JBoss application server. Then we have added a configuration tweak in undertow’s server to allow two Web applications to run on different ports.