Page tree
Skip to end of metadata
Go to start of metadata

This instruction will help you configuring Cocoon along with Tomcat for virtual hosting. We are going to serve several virtual hosts each from its own directory.

  Host                        Directory
  http://customerA/           /opt/virtHosts/customerA/
  http://customerB/           /opt/virtHosts/customerB/
  http://customerC/           /opt/virtHosts/customerC/         /opt/virtHosts/

Prepare the environment

You have installed and configured Cocoon and Tomcat according to CocoonEasyInstallation.


Now create the following directories


Make sure your Nameserver knows about all the hosts. If you are on a local machine without nameserver you can add them to /etc/hosts file.
Put some sample which you know that is working into each customerX directory.

Define HostMatcher

The default sitemap already has many Matchers defined but there are some more that we could add.
Backup your /opt/cocoon/ROOT/sitemap.xmap and open it with your favourite editor. Search for the section

   ...other components...
   <map:matchers default="wildcard">
     ...other matchers...
   ...other components...

and add the line

    <map:matcher logger="" name="host" src="org.apache.cocoon.matching.WildcardHostMatcher"/>

so that you now have

   ...other components...
   <map:matchers default="wildcard">
     ...other matchers...
     <map:matcher logger="" name="host" src="org.apache.cocoon.matching.WildcardHostMatcher"/>
     ...other matchers...
   ...other components...

Now as our sitemap.xmap knows about WildcardHostMatcher we can use it in our pipeline.

Use HostMatcher (in the pipeline)

Search for the section <map:pipelines> and find the <map:pipeline> which is commented "main pipeline".

   <!-- main pipeline -->

and directly below, and still above the welcome pages, add the lines:

    <!-- Insertion for matching http://cocoon:[portnumber]/ -->
    <map:match pattern="cocoon:**" type="host">

Further down in the pipeline, just before beginning of comment about error handling <map:handle-errors> put the folllowing lines of which the first is the closing tag of our previous insertion from above.

        | Mount everything else by calling the sitemap.xmap file located
        | in the requested folder.
        | First match, pattern="*:**" is for http://virthost:8080/uri
        | Second one, pattern="*" is for calling at default port.
    <map:match pattern="*:**" type="host">
      <map:mount check-reload="yes" src="/opt/virtHosts/{1}/" uri-prefix=""/>
    <map:match pattern="*" type="host">
      <map:mount check-reload="yes" src="/opt/virtHosts/{1}/" uri-prefix=""/>

Save your changes to sitemap.xmap.

http://cocoon:8080/ or http://cocoon/

Be aware that we added one new matcher matching *http://cocoon:\[portnumber\]/* nested with all other matches from the original pipeline.
Please change it from pattern="cocoon:**" to pattern="cocoon" if you've connected Tomcat to Apache or if you configured Tomcat to listen on port 80. Restart Tomcat and start your browser with *http://customerA/* or another.


Setting up a new virtual host requires just only creating in /opt/virtHosts/ a directory with its name being the same as the host name.

Feel free to put any comments or corrections or to contact the author for questions under

Related stuff

Of course, Cocoon has a RegexpHostMatcher too and also a HostSelector which already got its own wikipage HostSelector.

If you prefer having Apache in front to match the (virtual) hosts, please have a look at Stavros Kounis' VirtualHost.

  • No labels