Virtual host definitions in Tomcat 4.x have to be provided in the server configuration file, server.xml itself. Once these additions have been done, we simply need to restart Tomcat to use the virtual host definitions.
Editing server.xml
The default sample server.xml file of the Tomcat 4.x build contains two services - one for the standalone server, and one for the server that cooperates with an Apache web server using the WARP protocol. We can remove the definition for the second service and reuse that of the first service.
The top-level <Service> element for the standalone server would look like the following:
The rest of the configuration would go inside this <Service> container element. We then add the Connectors to be used for this service. Since this is a standalone server, we only need the HTTP/1.1 connector to communicate with the outside world.
We add the following Connector definition inside the <Service> element.
This sets up Tomcat to listen to port 8080 for incoming web requests.
We now add the <Engine> element to the <Service> by adding the following lines just after the <Connector> element and inside the <Service> element:
This specifies an engine for the service that processes incoming requests from the connectors. After any request has been received by the connector and passed on to the engine, the engine would then take a look at the HTTP headers, especially the Host:tag, and determine which of the virtual host definitions that it handles would receive the request. If none of the virtual host seems to match the request headers, the engine passes on the request to a default host. The name of the default virtual host is specified in the attribute defaultHost. The value of this attribute must match a <Host> definition in the engine.
For our purposes, we see that the virtual host definition of europa.dom is served by default when a web request is made using the IP address (instead of a host name).
We next add the virtual host definition of europa.dom to the engine. We add the following content inside the <Engine> element:
This defines a virtual host entry for europa.dom in Tomcat. We further add some logging functionality to this virtual host by placing the following content within the <Host> element:
This defines two logging services for this virtual host. The <Logger> element is covered in Chapter 5.
We now add the contexts to serve for this virtual host, inside the <Host> element.
We have added two contexts here. The first one is the default context with an empty context path. This context either has to be defined explicitly or provided automatically by Tomcat (that is, without you defining it here) if you have a web application called ROOT in the appBase of the virtual host.
As an example of how new web applications other than the default one have to be added to the site, we have also added the web application called shop which uses the /shop context path.
One nice thing about context definitions in Tomcat 4.x is that it provides automatic contexts in case you haven't defined them in the host definition. To provide this functionality, Tomcat looks at directories inside the appBase directory. If these directories follow the web application structure, specifically if they contain a WEB-INF/web.xml file in them, Tomcat automatically provides contexts with context paths equal to the name of the directory under appBase.
Remember that the default parameters of these contexts are picked up from $CATALINA_HOME/conf/web.xml.
If you need to override some global parameters to these contexts, you need to place them within the <Context></Context> elements. Examples would be, logging for this context in a separate file, context parameters, resource definitions, and so on.
This completes the virtual host definition for europa.dom. For the virtual host callisto.dom, we add another virtual host entry similar to that of europa.dom:
We save this file as $CATALINA_HOME/conf/server.xml and restart the Tomcat service.
We first check the test JSP file in the europa.dom virtual host using the URL http://europa.dom:8080/test.jsp:
We then check out the callisto.dom using the URL http://callisto.dom:8080/test.jsp:
We then check whether the default host setting of the <Engine> element is working properly. For this we use a host name other than the ones specified explicitly as <Host> definitions. Let's try accessing using the IP address 10.0.0.1:
As we can see, Tomcat serves the contents of the europa.dom virtual host, as defined in the default virtual host entry of the engine.
Now that we have Tomcat 4.0 working as a standalone server for the virtual hosts, let's make it work along with Apache.
No comments:
Post a Comment