Tomcat is not only a servlet container. It supports many of the features of any HTTP web server and can be also used to serve static content as well. From my experience, it can handle heavy traffic quite decently. It doesn’t fall from it’s big brother: Apache Server, and frankly, there are times it seem to be functioning even better.
One of Tomcat’s features, is Virtual Hosting. Virtual Hosting means that a single web server can server more than one website at the same time. The server knows which website to server according to the domain of the request. For example, if you have 2 domains: http://one.com and http://two.com and you direct both domains to the same IP, the server will get requests from 2 different domains. Then it should know to serve “one” website to requests coming from domain http://onew.com and “second” website to requests coming from domain http://two.com.
Configuring Tomcat to support Virtual Hosting is an easy task. The configuration takes place in Tomcat’s server.xml file, which is placed under the “conf” directory. Inside the “Engine” xml tag, “Host” element should be added for every domain you would like to serve. For example, if we have a web application named “one” and a domain named: http://one.com, The “Host” element should look like:
Note that “one” is places under “webapps” which is Tomcat’s natural directory for web applications. Therefore, a relative path is used. In addition, take a look at the alias: www.one.com. That will allow the server to acept traffic from both: http://one.com and http://www.one.com.<Host name="one.com" appBase="webapps/one"><Context path="" docBase="" debug="0"/><Alias>www.one.com</Alias></Host>
Let’s have a look at a full server.xml example file, which defines 2 virtual hosts for 2 web applications: “one” and “two”:
<?xml version='1.0' encoding='utf-8'?><!--
Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.--><!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.Documentation at /docs/config/server.html--><Server port="8005" shutdown="SHUTDOWN"><!--APR library loader. Documentation at /docs/apr.html --><Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /><!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --><Listener className="org.apache.catalina.core.JasperListener" /><!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html --><Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" /><Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /><!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html--><GlobalNamingResources><!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users--><Resource name="UserDatabase" auth="Container"type="org.apache.catalina.UserDatabase"description="User database that can be updated and saved"factory="org.apache.catalina.users.MemoryUserDatabaseFactory"pathname="conf/tomcat-users.xml" /></GlobalNamingResources><!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",so you may not define subcomponents such as "Valves" at this level.Documentation at /docs/config/service.html--><Service name="Catalina"><!--The connectors can use a shared executor, you can define one or more named thread pools--><!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"maxThreads="150" minSpareThreads="4"/>--><!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)Java AJP Connector: /docs/config/ajp.htmlAPR (HTTP/AJP) Connector: /docs/apr.htmlDefine a non-SSL HTTP/1.1 Connector on port 8080--><Connector port="80" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" URIEncoding="UTF-8" disableUploadTimeout="true" compression="on" compressableMimeType="text/html,text/xml,text/plain,application/xml"/><!-- A "Connector" using the shared thread pool--><!--
<Connector executor="tomcatThreadPool"port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />--><!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, theconnector should be using the OpenSSL style configurationdescribed in the APR documentation --><!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"maxThreads="150" scheme="https" secure="true"clientAuth="false" sslProtocol="TLS" />--><!-- Define an AJP 1.3 Connector on port 8009 --><Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /><!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand aloneanalyzes the HTTP headers included with the request, and passes themon to the appropriate Host (virtual host).Documentation at /docs/config/engine.html --><!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">--><Engine name="Catalina" defaultHost="localhost"><!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)/docs/config/cluster.html (reference documentation) --><!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>--><!-- The request dumper valve dumps useful debugging information about
the request and response data received and sent by Tomcat.Documentation at: /docs/config/valve.html --><!--
<Valve className="org.apache.catalina.valves.RequestDumperValve"/>--><!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any editsthat are performed against this UserDatabase are immediatelyavailable for use by the Realm. --><Realm className="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase"/><!-- Define the default virtual host
Note: XML Schema validation will not work with Xerces 2.2.--><!--Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"xmlValidation="false" xmlNamespaceAware="false"--><!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html --><!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.
Documentation at: /docs/config/valve.html --><!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>--><!--/Host--><Host name="one.com" appBase="webapps/one"><Context path="" docBase="" debug="0"/><Alias>www.one.com</Alias></Host><Host name="two.com" appBase="webapps/two"><Context path="" docBase="" debug="0"/><Alias>www.two.com</Alias></Host></Engine></Service></Server>
Note, that I remarked Tomcat’s default “Host” element:
<!--Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"xmlValidation="false" xmlNamespaceAware="false"-->
Remarking the default “Host” element is necessary. Otherwise Tomcat will simply load each web application twice: one for the default host definition and the second for the virtual host definition.
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete