Record the configuration of virtual host in tomcat

March 19, 2017 1902 point heat 0 likes 0 comments

During development, the war package is directly thrown into the webapps directory of tomcat localhost:8080/project But it is obviously impossible to deploy the online system through www.xxx.com/project It is accessed in this way.

It was naive to think that the domain name could point to IP: port/project. Today, I went to the customer to deploy it and found that it was totally different. The domain name could only point to IP. So I immediately asked Douniang for help and found the following solution.

In tomcat's conf/server.xml The default node is

 <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true"></Host>

Localhost is the domain name of the local machine. You can see a sentence in the hosts file:

localhost name resolution is handled within DNS itself.

That is, the localhost points to the local machine, the IP address 127.0.0.1. You can add tomcat virtual hosts and bind domain names by modifying and adding Hosts.

In this experiment, I will put two war packages, one of which is admin.war , the corresponding domain name is admin.xxx.com One is sm.war , the corresponding domain name is www.xxx.com

When doing experiments on the local machine, you can simulate domain name access by modifying the hosts file. Here I will add three domain names

127.0.0.1 www.xxx.com
127.0.0.1 admin.xxx.com

Next, configure the virtual host in tomcat

First, describe the directory structure of the two hosts

 tomcat ├─admin │  └─war ├─sm │  └─war

Later, explain why the directory is created like this.

Add two virtual hosts in server.xml

 <Host name="www.xxx.com"  appBase="sm" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="./war/sm.war" /> </Host> <Host name="admin.xxx.com"  appBase="admin" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="./war/admin.war" /> </Host>

Explain the parameters

Host

Name The virtual host name, usually the domain name
AppBase virtual host application directory
unpackWARs
If it is true, the war package will be decompressed; otherwise, it will not be decompressed and run directly
AutoDeploy defaults to true, which means that if a new WEB application is placed in the appBase and Tomcat is running, the application will be loaded automatically.
context

Path web application name,
The specific storage path of the docBase web application
The role of path can be seen in the Tomcat principle written by this friend: http://blog.csdn.net/xiemk2005/article/details/5988764

After the configuration is completed, start tomcat and access two apps through the domain name.


Here are a few points to explain:

Why did you create the war directory when you just created the directory

If there is a war package in the root directory specified in appBase, tomcat will load it into memory, while the war package in the subdirectory will not. You can also throw the war package to other unexpected directories in the appBase, which can be specified in the docBase of Context.

Why not put the war package in webapps

Since my path configuration is "" (empty string), it will be decompressed to the ROOT directory by default. If it is placed in webapps, it will be decompressed to ROOT

Gcod

If life is just like the first sight, what is the sad autumn wind painting fan

Article comments