Configure different access ports for each web application under Tomcat

Source: Internet
Author: User
Tags website domain names

Configure different access ports for each web application under Tomcat

To complete this directory, you must configure the conf/server. xml file.

Now we have two apps, app1 and app2. The client expects the following access methods:

App1-> http: // localhost: 8081/

App2-> http: // localhost: 8082/

This saves the trouble of adding contextpath after the host name, and we believe the customer is more willing to use it like this.

Steps:

1. Find the service configuration section in CONF/server. XML, copy the service element, and paste it next to it. As follows:

<Servicename ="Catalina. app1">

<Connector Port ="8081& Quot; Protocol = & quot; http/1.1 & quot"

Connectiontimeout = "20000"

Redirectport = "8443" type = "regxph" text = "yourobjectname"/>

<Engine name ="Catalina. app1"Defaulthost ="Localhost">

<Realmclassname = "org. Apache. Catalina. realm. lockoutrealm">

<Realmclassname = "org. Apache. Catalina. realm. userdatabaserealm"

Resourcename = "userdatabase"/>

</Realm>

<Host name ="Localhost"Appbase ="Webapps"

Unpackwars = "true" autodeploy = "true">

<Valveclassname = "org. Apache. Catalina. Valves. accesslogvalve" directory = "logs"

Prefix = "localhost_access_log." suffix = ". txt"

Pattern = "% H % L % u % T & quot; % R & quot; % S % B"/>

<Context Path = "/" docbase = "app1"/>

</Host>

</Engine>

</Service>

Note: Red bold is a special part.

1.1, <servicename ="Catalina. app1"> Here, set the service nameCatalina. app1,The specified name cannot be the same as the existing service element of the current file.

1.2, <connectorport ="8081"... /, Set the HTTP ctor listening port of the current service to 8081. Note that it cannot be the same as the listening port of the existing connector of other services.

1.3, Catalina. app1 "defaulthost =" localhost ">, set the engine name to Catalina. app1 is not required either. You can change the name to another one, but do not duplicate the name with other engines. Defaulthost indicates the default VM to be accessed by the current engine based on its sub-element in the Host header. Here, there is only one virtual host in the engine, so the default host is also it. Of course, the host name specified by the defaulthost attribute must exist in its child element.

1.4. Localhost"Appbase ="Webapps"/>, Name attribute specifies the name of the VM, which is generally an Internet domain name. The engine matches the virtual host name according to the Host Header Value in the HTTP request, if the match exists, it is handed over to the VM for processing. For example, if your client access address is: http://qrkx.uten.cn: 8090/index. jsp, then the virtual host name here should be qrkx.uten.cn, of course, the port number in the request is first used by the front connector. The appbase attribute specifies where the web application on the VM is deployed. Multiple web applications can be deployed on a VM at the same time. Therefore, appbase indicates the storage location of these applications. Here, an absolute path with a drive letter can be used, such as D:/webapps. it can also be a relative path. The starting point of this relative path is the tomcat installation directory. Here, the appbase attribute is set to webapps, indicating that the Web application storage directory of the current VM is the webapps directory under the tomcat installation directory.

1.5, <Context Path = "/" docbase = "app1"/> This is the most critical part. You can deploy multiple Web Application, and each Web An application is Context Therefore, you can configure multiple Context Element, each representing Web ApplicationProgramContext. Path Attribute indicates accessing this Web Application Path, "/" Indicates this Context Is the default of the current VM. Web The application can also be a Null String "". Context You can use this function to process all non-matched items. Context Of Context path Request. We do not need to enter Web The Application name can be accessed as follows Http: // localhost: 80801/ If you configure it like this: <Contextpath = "/app1" docbase = "app1"/>, Then you need to access: Http: // localhost: 80801/app1/ . In summary, there is only one virtual host. Web The application uses the root path. For other applications, you must specify a special access path [only one root path]. For example: Path = "/app1" , Path = "/app2" , Path = "/app3" .

docbase = "app1" This attribute indicates the current Web application storage path, which can be relative or absolute, or a path with a drive letter , example: D:/app1, the relative path is host element appbase the path specified by the attribute is the start path. docbase = "app1" indicates our current Web the application is in Tomcat webapps app1 directory under the directory.

 

2. Repeat the steps to create a service multiple times

<Servicename ="Catalina. app2">

<Connector Port ="8082& Quot; Protocol = & quot; http/1.1 & quot"

Connectiontimeout = "20000"

Redirectport = "8443" type = "regxph" text = "yourobjectname"/>

<Engine name ="Catalina. app2"Defaulthost ="Localhost">

<Realmclassname = "org. Apache. Catalina. realm. lockoutrealm">

<Realmclassname = "org. Apache. Catalina. realm. userdatabaserealm"

Resourcename = "userdatabase"/>

</Realm>

<Host name ="Localhost"Appbase ="Webapps"

Unpackwars = "true" autodeploy = "true">

<Valveclassname = "org. Apache. Catalina. Valves. accesslogvalve" directory = "logs"

Prefix = "localhost_access_log." suffix = ". txt"

Pattern = "% H % L % u % T & quot; % R & quot; % S % B"/>

<Context Path = "/" docbase = "app2"/>

</Host>

</Engine>

</Service>

 

3. start Tomcat and observe the log output.

 

Information: deploying webapplication directory e: \ workspace \ javaworkspace \ Tomcat 7.0 \ webapps \ app1

2012-6-414: 03: 36 com. opensymphony. xwork2.util. Logging. commons. commonslogger info

Information: parsingconfiguration file [struts-default.xml]

2012-6-414: 03: 37 com. opensymphony. xwork2.util. Logging. commons. commonslogger info

Information: parsingconfiguration file [struts-plugin.xml]

2012-6-414: 03: 37 com. opensymphony. xwork2.util. Logging. commons. commonslogger info

Information: parsingconfiguration file [struts. xml]

2012-6-414: 03: 38 org. Apache. Catalina. startup. hostconfig deploydirectory

Information: deploying webapplication directory e: \ workspace \ javaworkspace \ Tomcat 7.0 \ webapps \ app2

2012-6-414: 03: 39 com. opensymphony. xwork2.util. Logging. commons. commonslogger info

Information: parsingconfiguration file [struts-default.xml]

2012-6-414: 03: 39 com. opensymphony. xwork2.util. Logging. commons. commonslogger info

Information: parsingconfiguration file [struts-plugin.xml]

2012-6-414: 03: 39 com. opensymphony. xwork2.util. Logging. commons. commonslogger info

Information: parsingconfiguration file [struts. xml]

 

4. access through a browser:

Http: // localhost: 80801/index. jsp

We observe that the request header sent by the browser is as follows:

Request URL:Http: // localhost: 8081/index. jsp

Request Method:Get

Status Code:200 OK

Request headersView Source

Accept:Text/html, application/XHTML + XML, application/XML; q = 0.9, */*; q = 0.8

Accept-charset:GBK, UTF-8; q = 0.7, *; q = 0.3

Accept-encoding:Gzip, deflate, SDCh

Accept-language:Zh-CN, ZH; q = 0.8

Cache-control:Max-age = 0

Connection:Keep-alive

COOKIE:JSESSIONID = cadaac6e5b9425f4590e50b889be58bf

Host:Localhost: 8081

Pragma:No-Cache

User-Agent:Mozilla/5.0 (Windows NT 5.1) applewebkit/535.19 (khtml, like gecko) Chrome/18.0.1025.168 Safari/535.19 qihu360ee

Response HeadersView Source

Content-Length:1235

Content-Type:Text/html; charsets = UTF-8

Date:Mon, 04 Jun 2012 06:24:19 GMT

Server:Apache-Coyote/1.1

Set-COOKIE:JSESSIONID = ba86ead5bbdf7bb51343377c88588ab8; Path =/; HTTPOnly

 

Note that the red part is the host header, which is generally an Internet domain name.

 

HTTP: /localhost: 80802/ognl. jsp

The Host header is as follows:Host:Localhost: 8082

5. Observe the conf directory of Tomcat:

What have you found ?????????????????????????????????????

Catalina. app1AndCatalina. app2Are the twoEngine. Open these two directories and we find that:

There isLocalhostIn fact, this is the name of the VM we configured above. If the name of the VM you configured isQrkx.uten.cnThe directory name isQrkx.uten.cn.

 

NOTE: If yourWebappsDirectory hasRootChange the directory name or delete it; otherwise, the access address: Http: // localhost: 8081AndHttp: // localhost: 8082There will be problems. The official documentation provides the following instructions:If the contextpath is a zero length string, the base name isroot

 

 

The Host header is used for demonstration. It is too troublesome to build a DNS server because the test environment does not have a DNS server. We can simulate it by modifying the following local file:

C: \ windows \ system32 \ drivers \ etc \ hosts, the role of this file:

Host is a system file without an extension and can be opened using notepad and other tools. Its function is to associate some common website domain names with their corresponding IP addresses with a "Database ", when a user enters a URL in the browser to log on to, the system will first automatically find the corresponding IP address from the hosts file. Once found, the system will immediately open the corresponding webpage. If not found, the system then submits the website address to the DNS domain name resolution server for IP Address Resolution.

Add the following to the file:

127.0.0.1 localhost

127.0.0.1 app1.family.cn

127.0.0.1 app2.family.cn

Then, modify the host headers of the VM in the server. xml file as follows:

<Servicename = "Catalina. app1">

<Connector Port = "8081" protocol = "HTTP/1.1"

Connectiontimeout = "20000"

Redirectport = "8443" type = "regxph" text = "yourobjectname"/>

<Engine name = "Catalina. app1" defaulthost ="App1.family.cn">

<Realm classname = "org. Apache. Catalina. realm. lockoutrealm">

<Realmclassname = "org. Apache. Catalina. realm. userdatabaserealm"

Resourcename = "userdatabase"/>

</Realm>

<Host name ="App1.family.cn"Appbase =" webapps"

Unpackwars = "true" autodeploy = "true">

<Valveclassname = "org. Apache. Catalina. Valves. accesslogvalve" directory = "logs"

Prefix = "localhost_access_log." suffix = ". txt"

Pattern = "% H % L % u % T & quot; % R & quot; % S % B"/>

<Contextpath = "" docbase = "app1"/>

</Host>

</Engine>

</Service>

<Service name = "Catalina. app2">

<Connector Port = "8082" protocol = "HTTP/1.1"

Connectiontimeout = "20000"

Redirectport = "8443" type = "regxph" text = "yourobjectname"/>

<Engine name = "Catalina. app2" defaulthost ="App2.family.cn">

<Realmclassname = "org. Apache. Catalina. realm. lockoutrealm">

<Realmclassname = "org. Apache. Catalina. realm. userdatabaserealm"

Resourcename = "userdatabase"/>

</Realm>

<Host name ="App2.family.cn"Appbase =" webapps"

Unpackwars = "true" autodeploy = "true">

<Valveclassname = "org. Apache. Catalina. Valves. accesslogvalve" directory = "logs"

Prefix = "localhost_access_log." suffix = ". txt"

Pattern = "% H % L % u % T & quot; % R & quot; % S % B"/>

<Contextpath = "" docbase = "app2"/>

</Host>

</Engine>

</Service>

 

Restart Tomcat and access the following address in the browser address bar:

Http://app1.family.cn: 80801/INDE. jsp

Http://app1.family.cn: 80802/ognl. jsp

Success !!!!!!!!!

 

Observe the conf directory in Tomcat:

 

It is not difficult to find that the virtual host name directory is generated under the directories of the two engines respectively.

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.