How to configure virtual hosts on Tomcat

With virtual hosts, you can serve different domain names on a single instance of Tomcat server. I will be describing the way to do this on this post.

To open a virtual host on Tomcat you need to do three things:

1. Create a appBase for each virtual host you will be adding
2. Edit the server.xml to add the virtual hosts
3. Edit the etc/hosts file to loopback on the virtual hosts

1. Creating an appBase is a very important step for our process. This is like creating multiple “webapps” directories for every virtual host that we have. Every appBase should contain a “ROOT” directory, which will contain all the stuff that we will be needing for providing on a virtual host. This can be servlets, html files, etc etc. Put anything you want to host, in that directory. So this will be the structure of your appBase directory after you complete the above step:

2. Server.xml tells the Tomcat server about the details of the virtual hosts. You can locate the server.xml file under conf directory of Tomcat. Find the following in the server.xml:

– <Engine name=”Catalina” defaultHost=”localhost”>

To this part, we should add our virtual hosts. Here is the way to do it:

– <Engine name=”Catalina” defaultHost=””>
<Host name=”” appBase=”vinayapps” />
<Host name=”” appBase=”chilakapps” />

Make sure that the defaultHost is one of the hosts that you defined in the Host tags. This will complete the configuration that is needed on the Tomcat, to run virtual hosts. But there is another important step that you need to perfom, to tell the browser (client) that it has to loop back to the local machine when you type in the URL that you just created. This is easy

3. In the final step, we should tell our client to connect to loop back to the localhost when we type in the virtual host name. This is a very easy step.

on Windows:
%root%:\WINDOWS\system32\drivers\etc and locate the “hosts” file in that. Edit the hosts file to something like this:
This will tell the machine that it has to loop back on the above domain. You can access the above domain only on the ports that you opened on the localhost. For example, the Tomcat comes with a default 8080 port connnector. To create multiiple connectors do this:
a. Open server.xml in your fav editor.
b. Find a tag like this on the xml file: <Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ />
c. Add another tag of that sort, now with your custom port. Ex: <Connector port=”666″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ />

Voila! you now have your virtual hosts on your favorite ports! Remember that opening a connector will expose all the appBases on the server.


16 Responses to “How to configure virtual hosts on Tomcat”

  1. After setting up apache on a linux machine. Our webpage can be accessed at the adress:

    In fact it can now also be accessed at the adresses:
    where,, have identical IP adresses

    I want the page to be accessible as and only as:

    The text “How to .. Tomcat” seems to adress this, although my favourite port is not 666, but 80. I could not get the propsed solution to work for me. Is defining a virtual host a solution for this indeed? or do I apply something else?
    Thanks in advance.

  2. vinaychilakamarri Says:

    First thing I want to ask you is whether it is Apache HTTPD or Apache Tomcat. The former has it’s own way of configuring virtual hosts(which I am planning to write about in the coming days). From what you posted, it appears to me that you have configured name based virtual hosts. Did you point all of them to a single docbase?

    I want the page to be accessible as and only as:

    You got me a little confused here. If you just want one link to point to a specific resource on your disk, why would you need virtual hosts at all?

  3. […] public links >> virtualhost How to configure virtual hosts on Tomcat Saved by vogelap on Thu 20-11-2008 Apache2 mod_rewrite y virtualhost Saved by vsiva on Thu […]

  4. Hi,

    I created the virtual hosts as you mentioned, and I am able to browse the homepage etc. So that part is working great!

    But I need to insert some stuff into a mysql database. I am not able to do that from the virtual hosts. Is there anything special to be done so that the jdbc is up and running?


  5. It actually works…just a stupic mistake that I had made!! 🙂


  6. Thanks.. for help..
    was very help full.. 🙂

  7. Ako Desk…

    […]How to configure virtual hosts on Tomcat « A coder’s blog for coders[…]…

  8. Safe harbor 401k rules…

    […]How to configure virtual hosts on Tomcat « A coder’s blog for coders[…]…

  9. κυτταρίτιδα…

    […]How to configure virtual hosts on Tomcat « A coder’s blog for coders[…]…

  10. Prestashop Themes…

    […]How to configure virtual hosts on Tomcat « A coder’s blog for coders[…]…

  11. Saravanan S Says:

    I have an application called portal in tomcat directory like
    portal —> pages, css, js
    –>> WEB-INF –>> web.xml

    i have domain called
    We have set up mod proxy & virtual host with apache
    then, Address bar shows the URL like

    How can we set up to

  12. i realize that is a very old post, not sure if it is still active.

    We did exactly what you have mentioned and yet we keep getting 404 error. Have ben trying for 2 days now, not even sure how to even begin debugging!

    Any help would be appreciated!

  13. Attractive component to content. I just stumbled upon your web
    site and in accession capital to claim that I acquire actually enjoyed account
    your weblog posts. Anyway I’ll be subscribing in your feeds or even I success you get entry to persistently fast.

  14. I have done the same thing as you wrote but nothing happened, it says 404 not found error.

    Server : Apache Tomcat 7.0

  15. May I simply say what a relief to discover somebody who genuinely
    knows what they’re talking about on the net. You actually understand how to bring an issue to light and make it important. More people need to check this out and understand this side of your story. I was surprised that you’re
    not more popular given that you surely possess the gift.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: