50

Currently I run single tomcat with single WAR application on port 80. The domain name www.foo.org is pointed to this server ip.

What is the procedure of adding www.bar.org domain for a different client on port 80?

Thank you

3 Answers 3

62

From the beginning you have a single "Host" record in your conf/server.xml for localhost

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
      <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

Now you can add another "Host" records, for example

  <Host name="anotherclient.com"  appBase="anotherclient" unpackWARs="true" autoDeploy="true">

       <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="anotherclient_access_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  </Host>

where name="anotherclient.com" is the new client's domain, and appBase="anotherclient" is its web application root directory name (where you deploy your war); it is relative to the tomcat home dir.

Changes will be accepted after tomcat is restarted.

Requests going to any other domains (not listed in server.xml) but pointing to IP address of your server will be passed to the default application, it is specified in the Engine element

<Engine name="Catalina" defaultHost="localhost">
9
  • 7
    Does this mean that the wars are at /opt/tomcat/webapps/ROOT.war for the original foo.org site, and then /opt/tomcat/anotherclient/ROOT.war for the new bar.org site?
    – CodeMed
    Oct 6, 2015 at 17:26
  • 1
    @CodeMed Yes, that is correct. I just tried this myself and putting the WARs like that seems to work for me. Jan 16, 2016 at 13:45
  • 1
    we have to create that directory anotherclient ourself or it's creating after restarting tomcat7
    – dollar
    Dec 15, 2016 at 3:01
  • 1
    An empty directory won't be very useful without war inside it, thus just create it and deploy your war there. Then restart tomcat.
    – humkins
    Dec 15, 2016 at 20:12
  • 2
    @Axel I have several hosts in the tomcats server.xml. Each looks like this: <Host name="www.my-url.xyz" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="my-web-app-1/2/3..." debug="0" privileged="true" /> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" /> </Host> Like this I map an url to an application, all under "webapps".
    – BAERUS
    Jan 25, 2019 at 17:24
6

In its default configuration, Tomcat accepts requests for any hostname and sends them all to the "localhost" <Host> defined in conf/server.xml. If you haven't change that, then all you have to do is make sure that you have DNS records set up for both hostnames to point to your server.

If you want to have a different set of webapps for each hostname, then you'll have to define a second <Host> in conf/server.xml and use the appropriate hostname for it. (Note that you'll always have to have a "default" host where all requests go that don't match any of the explicitly-defined hosts).

You can read the documentation for <Host> here: http://tomcat.apache.org/tomcat-7.0-doc/config/host.html (That's for Tomcat 7.0.x. You didn't mention your version so I gave you a link to the latest version's documentation).

3
  • Thank you - Its version 7, What about the WAR files? each client need a diffarent deployment? May 22, 2012 at 16:01
  • 1
    @user648026 Each <Host> gets its own appBase, so you can choose to put your WAR files in either or both of them. Choosing one will make that WAR file only accessible using the hostname of that <Host>, while putting the WAR file in both means that you will have two copies of the webapp running simultaneously in the same container: one for one host, and one for the other. May 22, 2012 at 16:18
  • So single tomcat running on port 80 can handle multiple hosts on the same port (80) with single WAR, using multiple "hosts" configuration. May 22, 2012 at 16:33
3

Usually you have a tomcat (or other application server) on port different from 80 (like 8080 or 7001 or anything you want). After that you put a web server (like apache http server) on port 80 and configure one or many connector to point to different port on different application server with different address.

For Apache http + tomcat you can take a look at this link:

http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html

4
  • 3
    There's no particular reason to add another product to the OP's environment: this problem can be solved using Tomcat alone. May 22, 2012 at 15:46
  • 1
    That's just a best practice in professional environment to have web and application server.
    – Guaido79
    May 22, 2012 at 15:56
  • 3
    Malarkey: Tomcat is a perfectly acceptable web server. Calling something a "best practice" does not make it a best practice. It's a /common/ practice, but certainly not a "best" practice. May 22, 2012 at 16:17
  • 3
    Another alternative to running Apache + Tomcat to forward traffic from port 80 to 8080 is to use NAT tables and translate all traffic coming on port 80 to port 8080. Even though Apache is fast NAT is faster and it is already a part of iptables (I am assuming your server is linux). Nov 4, 2012 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.