Tomcat server, which allows users to configure multiple Web sites on the same computer, in which case a different host name is configured for each Web site, that is, the virtual host is configured
In real life, in order to improve the utilization of hardware resources, there are many Web sites through the configuration of virtual host, the implementation of server sharing
In a tomcat server, configuring a virtual host requires the use of <Host> elements
Open the Server.xml file in the Conf folder under the Tomcat installation directory and find a line of code like the one shown below
<Host> element, which represents a virtual host
Its properties name and AppBase indicate, respectively, the name and path of the virtual host
Here, the name of the virtual host is localhost, and the path is the WebApps directory under the Tomcat installation directory
If, if you want to add a virtual host, only need in the Server.xml <Engine> element, add a <Host> element, the site is stored in the directory configuration, for the corresponding name of the host, you can
Like what
Configure the D:\newhost directory as a virtual host named Itcast
The code is as follows
<engine name= "Catalina" defaulthost= "localhost" > ...
The above code
A virtual host named Itcast is configured with the host element
<Host> element, there is a parent element <Engine>
An <Engine> element that is used to build an engine that handles client requests, accepts access requests sent by Tomcat's connectors, and, after specific processing, returns the results to the connector
The <Engine> element has a Defaulthost property that specifies the default virtual host, and if the host that is accessed does not exist, the default virtual host is accessed
Configure Itcast as the default virtual host
The code is as follows
<engine name= "Catalina" defaulthost= "Itcast" > ...
A configured virtual host that wants to be accessed by the outside world must also be registered in the DNS (domain name system--nameservers server) or Windows system, because when a URL is accessed through a browser, you need to specify the IP address of that host. To connect to the Web server by this IP
Therefore, when the virtual host configuration is complete, you also need to configure the mapping of the virtual host to the IP address in the Hosts file.
Typically, the Hosts file is located under the operating system root directory, typically in the C:\WINDOWS\SYSTEM32\DRIVERS\ETC directory
Open the Hosts file and find a line of text like the following
127.0.0.1 localhost
The purpose of this line of text is to set up an IP address, 127.0.0.1 native and host name localhost direct mapping, this is, in the Internet Explorer address bar, you can directly use localhost to access the local Web server reason
If, to increase the host name, the mapping relationship to the IP address
Only in this Hosts file, you can configure it
For example, configure the D:\itcast directory as a virtual host named Itcast
Configured as follows
127.0.0.1 Itcast Note
Don't add the # number in front, #号是注释的作用