Explain how the Tomcat server binds multiple domain names and virtual directories

  • 2020-05-14 05:39:46
  • OfStack

tomcat is installed by default. To bind multiple domain names to tamcat directly, we need to modify the configuration file :C:\Program Files\ Software Foundation\Tomcat 5.5\conf\ server.xml
The modification method of server. xml is as follows:
(1) multiple domain name binding
1. If you want to bind to a website, first change the default access port of tomcat, 8080, to 80
The original:


<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" />

Revised:


<Connector port="80" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" />

In fact, this is to change port:8080 to port:80, and the other parameters remain the same
Here's the point. Haha...
The original:


<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false" /> 
</Engine>

Of course I've deleted all the comment code here, which is an eyesore

Revised:


<Engine name="Catalina" defaultHost=[url]www.abc[/url].com>

<Host name="[url]www.abc[/url].com" appBase=="abcapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false" 

<Host name=[url]www.cba.com[/url] appBase=="D:\cba" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false" />

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"  />
</Engine>

Here is an explanation of the above configuration
dafaultHost of Engine: means to access the host that tomcat enters by default. Please note that 1 must not be localhost, otherwise, others will enter tomcat's management interface by default through your ip.
Host's name: represents the domain name bound to the host, which can be accessed by entering localhost in the browser if bound to localhost.
appBase of Host: represents the file storage path bound to this host, which can be either relative or absolute.
Follow the above configuration:
1. If I type [url]http://localhost[/url] in my browser, I visit the website under C:\Program Files\ Software Foundation\Tomcat 5.5\webapps\ROOT
2. If the input [url] http: / / www abc. com [/ url] access C: \ Program Files \ Apache Software Foundation 5.5 \ abcapps \ \ Tomcat ROOT under web site
3. If the input [url] http: / / www cba. com [/ url] access D: \ cba \ ROOT under web site.
Note that there is one ROOT directory to create, we just need to put the website in the corresponding ROOT directory down, we can access it through the corresponding domain name.
There are a lot of parameters here, I'm not sure, but it does allow multiple domain name binding. Besides, if the website page is modified, you can just directly overwrite it,tomcat can automatically update the class and page, of course, if you modify web.xml or lib, you need to restart tomcat.
(2) virtual directory


<Host name="localhost" appBase="webapps"
    unpackWARs="true" autoDeploy="true"
    xmlValidation="false" xmlNamespaceAware="false">
<Context path="/cqq" docBase="f:\java\cqqapp" debug="0" reloadable="true" crossContext="true"> 
  </Host>

Among them, the Host tag is used to configure the virtual host, that is, multiple domain names can point to 1 tomcat, as long as the format is referred to the default

Ok.

< context > It's a subelement of the Host tag, which means a virtual directory. It has two main attributes. path is the name of the virtual directory.

docbase is the specific file location. In this case, my virtual path name is cqq. In fact, my program is html, jsp,

servlet is listed in f:\java\cqqapp.

So I can through [url] http: / / 127.0.0.1 cqq / [/ url] access my the virtual directory.
The other one is:
Configure two sites


<Host name="[url]www.xyz.com[/url]" debug="0" appBase="D:\Tomcat5.5\portal" unpackWARs="true" autoDeploy="true"> 
<Context path="" docBase="D:/Tomcat5.5/portal" debug="0" reloadable="true"/> 
</Host>
<Host name="[url]www.abc.com[/url]" appBase="D:\Tomcat5.5\hxw"
    unpackWARs="true" autoDeploy="true"
    xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="D:\Tomcat5.5\abc" debug="0" reloadable="true"/> 
</Host>

With this setup, you can enter the domain name to both sites
Since no default site is specified, IP is not accessible when entered directly.
D:\ Tomcat5.5 \conf\Catalina\localhost\ ROOT.xml
The content such as


<Context path="/" docBase="${catalina.home}/portal" debug="5" reloadable="true" crossContext="true">
</Context>

So that we can realize the input domain, respectively, to a site, input IP will default to D: / Tomcat5. 5 / portal this site
But, but, there was a problem
These two sites are quite memory intensive to start, I have set the TOMCAT memory setting to 1400M (higher TOMCAT5 cannot be started), so I cannot start 3 applications at the same time.

You have not implemented, open 1 application and bind multiple domain name method.
As follows (TOMCAT is definitely not bootable, just to show what I mean)


<Host name="[url]www.abc.com[/url],192.168.0.1" appBase="D:\Tomcat5.5\hxw"
    unpackWARs="true" autoDeploy="true"
    xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="D:\Tomcat5.5\abc" debug="0" reloadable="true"/> 
</Host>

Since I'm integrated, I don't have to change port 8080


Related articles: