Tomcat Multi-port, multiple virtual host configuration method

Source: Internet
Author: User
Tags response code

Tomcat is used more in the development environment, but sometimes also in the production environment. In a production environment, you will encounter a situation where you need to place multiple sites under a tomcat. Sometimes you also need to open multiple different ports and accept multiple domain name requests. Typically, this can open multiple instances of Tomcat. This approach has the following advantages and disadvantages:

Advantages: 1. Each site is independent of each other and is free from interference. Other sites will not be affected because of a busy site.

2. You can make highly customizable for Tomcat. When it is necessary to do something unsafe for the container. This can be used to ensure that other sites are not affected. For example, you need to open a custom thread.

Disadvantages: 1. Waste system resources. Although Tomcat consumes little system resources, it is very serious that the instances are too wasteful. Because Tomcat has the problem of memory overflow, need to change large memory, and this leads to a lot of waste of system resources.

2. Management is not convenient, whether it is tuning, or day-to-day start-up, shutdown, port management. While Linux commands are handy, it's still a hassle.

I have found that on some servers, up to 6 Tomcat deployed, memory waste is conceivable ...

Sometimes I'm really envious of IIS on Windows, and I don't think a few of them have seen a single run of IIS on Windows. So I took a serious look at the Tomcat configuration, which actually Tomcat supports multiport, multiple virtual directories, multiple service ways, and visible tomcat configuration is flexible. Need to modify Conf/server.xml

The first is to have Tomcat listen on multiple ports:

<connector port= "8081" protocol= "http/1.1" connectiontimeout= "20000"
               redirectport= "8443

    "/> <connector port= "8082" protocol= "http/1.1" connectiontimeout= "20000"
               redirectport= "8443"/>

Just add a connector to it.

But the disadvantage of this approach is that two ports handle the same thing, that is, accessing 8081 and 8082 content is the same. This fit requires the use of a specified modular port.

The next step is to have Tomcat, a service for different domain names.

<engine name= "Catalina" defaulthost= "localhost" >

      <realm classname= " Org.apache.catalina.realm.UserDatabaseRealm "
	     resourcename=" Userdatabase "/> 

Just fill in a host and appbase need to use a different directory. So different domain names, access to different directories.

Finally, we might want Tomcat to be able to access different directories using different IP. In addition to the process is one, the other is the same as two instances. This XML is more than just a list of the parts you need.

<service name= "Catalina2" >

    <connector port= "8082" protocol= "http/1.1"
               connectiontimeout= "20000"
               redirectport= "8443"/>

    <engine name= "Catalina2" defaulthost= "localhost" >

      <realm className = "Org.apache.catalina.realm.UserDatabaseRealm"
             resourcename= "Userdatabase"/>

      

It needs to be added to the XML. is to add a service. Service name, Engine name,host appbase all need to be modified. This allows a tomcat to complete two instances of work.


Appendix: (Meaning of each parameter)
Service name Specifies the name of the service
Connector
(indicates connection between client and service) port specifies the port number to be created on the server side and listens for requests from the client on this fracture surface
Minprocessors number of threads created at server startup to process requests
Maxprocessors the maximum number of threads that can be created for processing requests
Enablelookups If True, you can obtain the actual hostname of the remote client by calling Request.getremotehost (), or false, and return its IP address instead of the DNS query
REDIRECTPORT specifies that the server is processing an HTTP request and receives a port number that is redirected after an SSL transfer request
ACCEPTCOUNT Specifies the number of requests that can be placed in the processing queue when all available processing requests are used, and requests that exceed this number will not be processed
ConnectionTimeout specify the number of times to timeout in milliseconds
Engine
(Represents a request processor in a specified service, receives and processes requests from connector) defaulthost Specifies the host name of the default processing request, which is at least the same as the Name property value of one of the host elements
Context
(Represents a Web application, usually a war file, specific information about the war, see Servlet specification) docBase The path of the application or the path to which the war file resides
Path represents the prefix of the URL for this Web application, so the URL for the request is http://localhost:8080/path/****
Reloadable This property is important, and if true, Tomcat automatically detects changes in the application's/web-inf/lib and/web-inf/classes directories, automatically loading new applications, We can change the application without having to restart Tomcat.
Host
(represents a virtual host)
Name Specifies host name
AppBase Application Base directory, that is, the directory where the application resides
Unpackwars if True, Tomcat automatically extracts the war file, otherwise it does not understand the pressure and runs the application directly from the war file
Logger
(indicates logs, debugging, and error messages)
CLASSNAME specifies the class name used by Logger, which must implement the Org.apache.catalina.Logger interface
prefix specifies the prefix of the log file
suffix specifies the suffix of the log file
Timestamp if true, the log file name is added to the time, as in the following example: Localhost_log.2001-10-04.txt
Realm
(a database that holds user names, passwords, and role)
CLASSNAME specifies the class name used by realm, which must implement the Org.apache.catalina.Realm interface
Valve
(Functionality similar to logger, whose prefix and suffix properties are interpreted in the same way as in logger)
CLASSNAME specifies the class name used by valve, such as the use of Org.apache.catalina.valves.AccessLogValve classes to record access information for an application

directory specifies where log files are stored
Pattern has two values, common way to record the remote host name or IP address, username, date, the first line of the requested string, HTTP response code, the number of bytes sent. Combined is more than the common way to record

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.