Virtual hosting with Jboss 5.1

How do I map a web application to a url in jboss?

If you have multiple web apps deployed in a single jboss instance, you’ll probably want to figure out an effective way to tell them apart when you try to access them from a browser. On startup jboss can be configured to bind to a single url which will act as the default host for all the deployed applications. You can then set up a separate context for each web app you are running. If they’re totally separate applications though, it might not make sense to use a single url and break them out by context. In Jboss you can set up virtual hosts to solve this dilemma. Here’s how to set this up:

WEB-INF/jboss-web.xml

In your web application you’ll want to add an xml file named “jboss-web.xml” to your WEB-INF folder. This is the file that’s going to map both the web application’s context and host in jboss.

<jboss-web>
    <context-root>/</context-root>
    <virtual-host>www.first-application.com</virtual-host>
</jboss-web>

This configuration sets the application’s context to “/” (essentially the root of the default domain), and it also maps the virtual host configuration to “www.first-application.com”. Note that it wont matter if you deploy this configuration from within an ear (embedded war file) or as a standalone war file, as only wars are meant to respond to web requests. Let’s also add the second configuration to the other war file’s WEB-INF/jboss-web.xml:

<jboss-web>
    <context-root>/</context-root>
    <virtual-host>www.second-application.com</virtual-host>
</jboss-web>

Next, we’ll need to add the virtual host configurations to jboss’ server.xml.

jbossweb.sar/server.xml

Now we need to edit jboss’ server.xml file, adding the virtual host mappings:

<Server>
   <Service name="jboss.web"
      className="org.jboss.web.tomcat.tc5.StandardService">
       
      <!-- A HTTP/1.1 Connector on port 8080 -->
      <Connector port="8080" address="${jboss.bind.address}"
                 maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                 enableLookups="false" redirectPort="8443" acceptCount="100"
                 connectionTimeout="20000" disableUploadTimeout="true"/>

      <Engine name="jboss.web" defaultHost="www.first-application.com">
         <Realm className="org.jboss.web.tomcat.security.JBossSecurityMgrRealm"
          certificatePrincipal="org.jboss.security.auth.certs.SubjectDNMapping"
            />
         <Logger className="org.jboss.web.tomcat.Log4jLogger"
                 verbosityLevel="WARNING"
                 category="org.jboss.web.localhost.Engine"/>

            <Host name="www.first-application.com" autoDeploy="false"
                  deployOnStartup="false" deployXML="false">
                <Alias>dev.first-application.com</Alias>
                <Alias>qa.first-application.com</Alias>   
                <Alias>test.first-application.com</Alias>    
                <Valve className="org.apache.catalina.valves.AccessLogValve"
					   prefix="localhost_access_log." 
					   suffix=".log"
					   pattern="common" 
					   directory="${jboss.server.log.dir}" 
					   resolveHosts="false" />
            </Host>   

            <Host name="www.second-application.com" autoDeploy="false" 
                  deployOnStartup="false" deployXML="false">
                <Alias>dev.second-application.com</Alias>
                <Alias>qa.second-application.com</Alias>   
                <Alias>test.second-application.com</Alias>    

                <Valve className="org.apache.catalina.valves.AccessLogValve"
					   prefix="localhost_access_log." 
					   suffix=".log"
					   pattern="common" 
					   directory="${jboss.server.log.dir}" 
					   resolveHosts="false" />
            </Host>

      </Engine>
   </Service>
</Server>

Note the alias map other optional domains that jboss would listen for as aliases of the keyed domains “www.first-application.com” and “www.second-application.com”. All this means is jboss will redirect requests for processing to the respective web application’s whose virtual-host config maps to this host configs.

In order for all of this to work however, we need to make sure dns is set up to handle these domain requests. On a local development machine, you’ll want to edit your hosts file (on windows: c:/WINDOWS/System32/drivers/etc/hosts, on linux: /etc/hosts) and add these entries:

127.0.0.1		www.first-application.com
127.0.0.1		www.second-application.com

This way when you type in one of the domains into your browser, it’ll forward the request to jboss’ bound IP. likewise, if you have jboss bound to a specific domain name/IP on boot, you would have to map that domain/ip in your hosts file just like in the example above.

Now you should be able to fire up jboss, type in either domain into the browser, and have jboss redirect those requests to the corresponding deployed war file.



Related posts:

  1. Configuring Data Sources, JBoss 7
  2. The conf directory, JBoss v5.x
  3. External deploy directories in JBoss

Comments (12)

  1. avatar

    3:16 am, June 18, 2010saujana  / Reply

    hi Ant,
    could you help me with the ip based virtual host configuration?
    is the steps similar to what you’ve shown ?
    what i tried to do is
    a.a.a.a will point the the web app a.war
    b.b.b.b will point to the web app b.war
    but the this is so far a.a.a.a can also view the web app b.war

    i really appreciate if you can help me out since im just started to use jboss
    Regards
    saujana

    • avatar

      10:23 pm, June 19, 2010Ant  / Reply

      Hi Saujana,
      Binding IPs is problematic as the IPs are usually bound to the machine’s network card and are not usually used to mark individual applications. Domains are used more frequently for this purpose. The problem stems from jboss binding to only one IP or domain on startup. Well, thats how I’ve usually done thing. Although, if you don’t bind jboss to an IP or domain on startup it should then listen on all IPs bound to it. So maybe if you don’t give it an address to bind, and then assign those IPs in the host configs and in the jboss-web.xml to match, that might get you what you’re looking to do. You’ll need to set up those ip aliases on the network card too but I’m assuming that’s already set up.

      Good Luck

  2. avatar

    11:29 pm, June 20, 2010saujana  / Reply

    good morning Ant,
    thanks a lot for the answer
    it’s really helpful

    Regards,
    saujana

  3. avatar

    5:04 am, July 23, 2010anandan  / Reply

    It is very useful for me. they given example code also very useful for me. i am really thank u for pulblish this code here.

  4. avatar

    4:27 pm, October 30, 2010Mister Woody  / Reply

    Nice post thanks. One query though – should the host file settings be 127.0.0.1 rather than 120.0.0.1?

    Thanks

    • avatar

      10:19 pm, November 1, 2010Ant  / Reply

      Indeed, thanks for catching that.

  5. avatar

    9:21 am, September 9, 2011Samer Gad  / Reply

    Hi Ant,

    First, sorry for replying on an old post, but it was very useful and I still want to ask for a further detail. How can I change the code-base for a specific virtual host?

    Best Regards,
    Samer

    • avatar

      9:37 am, September 9, 2011Ant  / Reply

      I’m not sure what you mean by changing the code base for a specific virtual host? Do you have multiple wars, multiple domains, or both? What are you trying to do?

  6. avatar

    11:19 am, December 9, 2011genci  / Reply

    Hi,
    I am new to this, so I would really appreciate your help. I have deployed a third party tool using jboss. I can access the site using https://localhost:8443/index.html. Instead of localhost:8443 I would like users to enter http://www.test.com/index.html (for example). I did try your suggestion, but can’t get it to work. Any suggestions?

    Thanks

    • avatar

      1:11 am, January 4, 2012Ant  / Reply

      Assuming you’re using jboss 5.1, if you’re accessing jboss through 8443 then you’re accessing the alternate https port. Check your /jboss-5.1.0/server/standard/deploy/jbossweb.sar/server.xml file and make sure this section of the xml file is not commented out:



      connectionTimeout="20000" redirectPort="8443" />

      If it’s not commented out, and you can access your site off port http://localhost:8080/index.html, then you can just change this port number from 8080 to 80. Ideally you won’t have jboss serving up any static content if you are running a large site. In those setups you’ll want to run something like apache to serve up your static content and then have it redirect dynamic content to jboss for processing. For smaller sites, this isn’t much of a big deal but it never hurts to be efficient.

      Good luck

  7. avatar

    1:16 pm, October 1, 2012Justin  / Reply

    I have the following problem:

    We have installed Software Quality Center by HP which is been installed on Joss server both as application and web servers.

    To access the start page I want instead of:

    https://abcd:8443/qcbin/

    I want to access like https://qc

    Could you please let me know what changes i need to male. I tried the above steps it is not working

    Best regard in advance
    Justin

    • avatar

      11:33 am, October 11, 2012Ant  / Reply

      you basically want to serve up your qc site with a web server like apache on the front end, and Install mod_jk. You can then set up an apache virtual host that points to the url: https://abcd:8443/qcbin/ and have https://qc as an alias. On the jboss side you will need to make sure the instance is listening for the mapped domain. Putting these two pieces together will get you what you’re looking for.

Pingbacks (0)

› No pingbacks yet.