Configuration of Tomcat's Server.xml virtual host and virtual directory

Source: Internet
Author: User

Previously developed Java EE sites are arranged under Tomcat, the layout directory is generally $catalina_home/webapps/webname, so to visit the site, then in HTTP// After localhost, the context path (ContextPath)/webname must be added, while the run environment is generally http://www.abc.com and the context path is empty. This creates the difference between the development environment and the operating environment, although the JSP can be solved by <%=request.getcontextpath ()%>, but if you change to another template, such as velocity, it will not work.

This article through the reference to the practice of the major netizens and their own hands-on practice, successfully set up a number of virtual host and virtual directories under Tomcat, write out for the reference of all users and make a note for themselves.

The configuration files for Tomcat 5 and Tomcat 6 are: $CATALINA _home/conf/server.xml, other versions of the Tomcat configuration file are not used.

Note the following one, two, ... The steps are not sequential and can be selected.

Description: Virtual Host in Server.xml is host, virtual directory is context.

First, modify the port
Tomcat Default Web port is 8080, I generally like to change it to 80, of course, it is completely unnecessary
<connector port= "protocol=" http/1.1 "connectiontimeout=" 20000 "redirectport=" 8443 "/>
Note that the HTTP protocol is modified, that is, the protocol= "http/1.1", not the other.

Second, increase the virtual directory
1, the simplest way: directly put the Java EE site in the $catalina_home/webapps directory, and then through the Http://localhost/WebName,WebName for the site directory name;

2, Custom: Put the site in other directories, such as in the/var/www/webapp directory, then you need to modify the Server.xml.
Add the following to </Host> before you:
<context path= "/springmvc" docbase= "/var/www/webapp/springmvc" workdir= "/var/www/webtmp/springmvc" debug= "5" Reloadable= "false" crosscontext= "true"/>
can be accessed through Http://localhost/SpringMVC, that is, the host name of localhost added a virtual directory named Springmvc, of course, the name here is entirely personal-dependent.

Third, increase the virtual host
In fact, this is the focus of this article, that is, the case at the beginning of this article. Each of us develop a Web site, are accessed through the form of http://localhost/WebName, is not very troublesome?

1. Modify the Hosts file to increase the host name
(1) The Hosts file for the Linux system is/etc/hosts
(2) The Hosts file for the WINDOWS system is C:\WINDOWS\system32\drivers\etc\hosts
For example, if you want to increase the www.local.com host, add a line to this file: 127.0.0.1 www.local.com, which means that when you visit www.local.com, you actually access 127.0.0.1;
I prefer a short domain name, so I added: 127.0.0.1 springmvc

2. Modify the configuration file
Add a Host node after </Host>:
That is, the addition of a virtual host named Springmvc, of course, it can also be name= "www.local.com", depending on the specific circumstances.
In this way, the IP access is: Http://SpringMVC.

3. Directory Settings
Host SPRINGMVC Web files are all placed in the/VAR/WWW/WEBAPP/SPRINGMVC directory, add the root directory under this directory, put into your Java EE website project, so that you can access your site through Http://SpringMVC , there is less/springmvc this string, on the IP and the operating environment to maintain the same.

Iv. Other issues

(1)

The SPRINGMVC website is accessed through Http://SpringMVC;
The website that is accessed by http://localhost is Tomcat's own;
We already know in the hosts file that the IP corresponding to both SPRINGMVC and localhost is: 127.0.0.1
So, what was the site that was accessed through http://127.0.0.1?

The parent node in host is: <engine name= "Catalina" defaulthost= "Springmvc" >
As can be seen from the above, defaulthost= "Springmvc" determines which site 127.0.0.1 visits, and my settings are Access Springmvc

This article is about to end, paste my server.xml file content:
<?xml version= "1.0" encoding= "Utf-8"?>
<server port= "8005" shutdown= "Shutdown" >
<listener classname= "Org.apache.catalina.core.AprLifecycleListener"
Sslengine= "on"/>
<listener classname= "Org.apache.catalina.core.JasperListener"/>
<listener classname= "Org.apache.catalina.mbeans.ServerLifecycleListener"/>
<listener classname= "Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>

<GlobalNamingResources>
<resource name= "Userdatabase" auth= "Container"
Type= "Org.apache.catalina.UserDatabase" description= "User database that can be updated and saved"
factory= "Org.apache.catalina.users.MemoryUserDatabaseFactory"
Pathname= "Conf/tomcat-users.xml"/>
</GlobalNamingResources>

<service name= "Catalina" >
<connector port= " the"Protocol=" http/1.1 "connectiontimeout=" 20000 "
redirectport= "8443"/>
<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>
<engine name= "Catalina" defaulthost= "Springmvc">
<realm classname= "Org.apache.catalina.realm.UserDatabaseRealm"
Resourcename= "Userdatabase"/>
Autodeploy= "true" xmlvalidation= "false" Xmlnamespaceaware= "false" >
<!--hellomule-->
<!--
<context path= "/mule" docbase= "/var/www/webapp/mule"
Workdir= "/var/www/webtemp/mule" debug= "5" reloadable= "false"
Crosscontext= "true"/>
-
</Host>
Unpackwars= "true" autodeploy= "false" xmlvalidation= "false"
Xmlnamespaceaware= "false"/>
</Engine>
</Service>
</Server>

(2)

First, modify the $tomcat/conf/server.xml file.
In the Server.xml file, there is a section as follows:
XML code

<engine name= "Catalina" defaulthost= "localhost to your domain" >

To add between XML code <context path= "" docbase= "myjsp" debug= "0" reloadable= "true"/>
Path is the name of the virtual directory, if you want to enter only the IP address to display the home page, the key value is left blank;
Docbase is the path to the default directory, it defaults to the $tomcat/webapps/root directory, and now I have a myjsp directory in the WebApps directory, which is the default directory for my directory.
Debug and reloadable are generally set to 0 and true respectively.

Configuration of Server.xml virtual hosts and virtual directories for Tomcat

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.