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:

120.0.0.1		www.first-application.com
120.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.

 
    Twitter
  • del.icio.us
  • Reddit
  • Technorati
  • Google Bookmarks
  • Blogplay
  • Yahoo! Buzz
  • LinkedIn
  • Facebook
  • Digg

Related posts:

  1. Install mod_jk
  2. War deployment file structure
  3. Set up Jboss 5.1 as a service
Posted on January 17, 2010 at 11:13 am by Ant · Permalink
In: Jboss · Tagged with: , , , ,

4 Responses

  1. saujana Written by saujana
    on June 18, 2010 at 3:16 am
    Permalink

    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

    [Reply]

    Ant

    Ant 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

    [Reply]

  2. saujana Written by saujana
    on June 20, 2010 at 11:29 pm
    Permalink

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

    Regards,
    saujana

    [Reply]

  3. anandan Written by anandan
    on July 23, 2010 at 5:04 am
    Permalink

    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.

    [Reply]

Subscribe to comments via RSS

Leave a Reply