Tomcat Virtual Host Configuration

Source: Internet
Author: User

1. Do not configure context tags

First we need to configure the host file, the path is C:\Windows\System32\drivers\etc\hosts, the file is the IP address and the domain name Mapping file, add the following code in the file:

127.0.0.1 www.kevin.com

This way, when we enter http://www.kevin.com in the browser, the host we find is the local machine [1].

Then open the Server.xml file with the path $catalina_home/conf/server.xml and add the following code to the file:

<!---      < name = "www.kevin.com"   AppBase= "Web_virtual_host"            unpackwars= "true"  autodeploy= "true"  >                  </Host>

We want to understand the role of several nodes and attributes:

(1) <Host></Host> node

That is, the host means that multiple virtual hosts are supported in Tomcat

(2) Name property

This is the unique identity when configuring multiple virtual hosts, filled in with a domain name, indicating that when the domain name is accessed, it will go to the corresponding IP virtual host under the search for Web applications.

(3) AppBase property

This is the application root of the virtual host, which contains the web apps that need to be deployed. The pathname can be an absolute path, or a relative path relative to the $catalina_base directory. If not specified, the default is WebApps.

(4) Unpackwars property

If true, the war packet in the AppBase directory will be automatically decompressed

(5) Autodeploy

If true, Tomcat periodically checks the AppBase and Xmlbase directories and deploys the new Web app or XML file found on it. Updating a web app or XML file triggers a reload of the web App

We add a directory web_test under the AppBase directory (that is, web_virtual_host), the directory will be used as a Web application to be deployed, and then a hello.html file is added to the Web_test directory as follows:

< H1 > This is a virtual host<H1>

We can also add a web-inf directory under the Web_test directory, and then add a Web. xml file under the Web-inf directory (which can be copied from the WebApps root directory), and then modify the code as follows:

< welcome-file-list >    < Welcome-file >hello.html</welcome-file>  </  Welcome-file-list>

This allows you to set the first page to hello.html

The entire directory structure is as follows:

Start Tomcat, enter http://www.kevin.com:8080/web_test/in the browser, and the results are as follows:

2. Configure the Context tab and the war package [1] path configured in Docbase

First configure the Hosts file.

Then modify the Server.xml file and add the following code:

<!--The virtual host -      <Hostname= "Www.kevin.com"AppBase= "Web_virtual_host" unpackwars= "true"Autodeploy= "true">                        <ContextPath=""DocBase= "/web_test" />                  </Host>

Where the properties of the <Context></Context> node are necessary to illustrate:

(1) Attribute Docbase

Specifies the path name of the document root or war file for the Web application, you can specify the absolute pathname of the directory or war file, or you can specify the relative pathname of the AppBase directory relative to the host element.

Here we configure the/web_test, which means that the Web_test.war file in the Web_virtual_host directory

(2) Attribute path

The context path of the Web app to run the appropriate Web app by matching the URI. The context path in a host must be unique. If you specify a context path as an empty string (""), the default Web app for this host (that is, a root folder) is defined, and is used to handle all requests that are not assigned to other Web apps (that is, if the Web app is not found, execute the default Web App, That is root)

Start Tomcat, you can see the appbase directory (ie web_virtual_host) under the Web_test.war is actually extracted, but was extracted into two folders, as follows:

Open Web_test and Root, you will find the file is the same, this is why?

This is because Tomcat must have a default access directory root in the AppBase directory, and if not, Tomcat extracts the war file that docbase in <Context> to the root folder. Also, because the files in the AppBase directory are considered web apps by Tomcat, and we set the property Unpackwars in host to True, Tomcat automatically extracts the Web_test.war into Web_test.

This means that Web_test has been deployed two times, so it is recommended that you put the war package in the level two directory, like this:

$CATALINA _home\web_virtual_host\war\web_test.war

The Server.xml file at this point is [2]:

<!--The virtual host -      <Hostname= "Www.kevin.com"AppBase= "Web_virtual_host" unpackwars= "true"Autodeploy= "true">                        <ContextPath=""DocBase= "/war/web_test" />                  </Host>

Starting Tomcat, the files under the AppBase directory (that is, web_virtual_host) are as follows:

At this point, in addition to the Web_test.war War folder, there is only the root folder, root files are Web_test.war extracted after the file. and enter http://www.kevin.com:8080/in the browser at this point, the result is shown as follows:

Note:

[1] command to fight a war package

Switch to the Web App current directory, go to directory, JAR-CVF Xx.war *, such as:

[2] It should be noted that the war package is placed in the WebApps directory, Tomcat will automatically unzip the war package and deploy the extracted web app, but if you put the war package in a different directory, Tomcat will not help you automatically decompression. So if you want to put it in another directory, you can only place the extracted web app and then configure the context (that is, configure a virtual directory) as follows:

<!--The virtual host -      <Hostname= "Www.kevin.com"AppBase= "Web_virtual_host" unpackwars= "true"Autodeploy= "true">                        <ContextPath=""DocBase= "/war/web_test" />            <ContextPath= "/web_test"DocBase= "/war2/web_test" />                  </Host>

Where docbase represents the path to the Web app, path represents the Web app name, and the directory structure is as follows:

Because we set Autodeploy to True, Tomcat automatically deploys the web App. We entered http://www.kevin.com:8080/web_test in the browser, the result is as follows:

1. Deploy the Web app when Tomcat starts

Translation:

Http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html#Deployment_on_Tomcat_startup

Deployment on Tomcat Startup

If you is not interested in using the Tomcat Manager, or TCD, then you'll need to deploy your Web applications statically To Tomcat, followed by a Tomcat startup. The location of your deploy Web applications to for this type of deployment are called the AppBase which is specified per Host. Either copy a so-called exploded Web application, i.e non-compressed, to this location, or a compressed web Applicati On resource. WAR file.

"If you're not interested in deploying Web Apps using Tmocat Manager or TCD, you can put the extracted web app or the war package into the AppBase directory of the corresponding host so that they will be deployed to the server as Tomcat starts"

The Web applications present in the location specified by the Host's (default Host is "localhost") appBase attribute (DEFA Ult appBase is "$CATALINA _base/webapps") would be deployed on Tomcat startup only if the Host ' s deployonstartup attribute I S "true".

"Only if the Deployonstartup property in <Host> is true, the Host will be deployed to the server for the Web app under AppBase as Tomcat starts." The default host is localhost, and the default appbase path is $catalina_base/webapps

Note: Deployonstartup default is True "

The following deployment sequence would occur on Tomcat startup in this case:

"When Tomcat starts, the order of deployment is as follows:"

Any Context descriptors would be deployed first.

The context file will be deployed first

Note:

The locations for Context descriptors is:

$CATALINA _base/conf/[enginename]/[hostname]/[webappname].xml

$CATALINA _base/webapps/[webappname]/meta-inf/context.xml "

Exploded Web applications not referenced by any Context descriptor would then be deployed. Note that if the exploded Web application has an associated. WAR file in the AppBase, Tomcat would not detect if the associated. The WAR had been updated while Tomcat was stopped and would deploy the exploded Web application as is. The exploded Web application is not being removed and replaced with the contents of the updated. WAR file.

"Then deploy the Web app that is not involved in the context file and has been unzipped. It's important to note that If the extracted web App has a corresponding. war file in the AppBase directory, the Tomcat runtime does not detect if the war file was updated after Tomcat stopped, but instead displays the extracted web app, and the content of the extracted web App does not change as the war file is updated 】

. WAR files would be deployed

"The last deployment is a. War file"

Tomcat Virtual Host Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.