Server.xml configure Tomcat virtual hosts and virtual directories

1 server.xml virtual hosts and virtual directories

Previously developed JavaEEsites are arranged Tomcat, the directory is usually arranged $CATALINA_HOME/webapps/WebName, so to access the site, then http://localhostthe context path must be added (ContextPath)/WebName, and the operating environment generally http://www.abc.com, the context path is empty.
This resulted in differences in the development environment and runtime environment, although JSPby the <%=request.getContextPath()%>resolve, but if replaced by other modules, for example Velocity, it has not.

Tomcat 5And Tomcat 6profiles are: $CATALINA_HOME/conf/server.xmlOther versions of the Tomcatconfiguration file with this
说明:virtual host server.xmlfor the Hostvirtual directory Context.

1.1 modify the port

TomcatThe default WEBport8080

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

注意It is to modify the HTTPagreement, that is protocol="HTTP/1.1", not the other.

1.2 increase the virtual directory

1.2.1 The easiest way

Directly to the JavaEEwebsite on the $CATALINA_HOME/webappsdirectory, and then http://localhost/WebName, WebNameas the name of the directory site;

1.2.2 Custom

The site's directory on the other, such as in /var/www/webappthe directory, it is necessary to amend server.xmlthe.
In </Host>add the following before you can:

<Context path="/SpringMVC" docBase="/var/www/webapp/SpringMVC" workDir="/var/www/webtmp/SpringMVC" debug="5" reloadable="false" crossContext="true" />

You can at http: Access // localhost / SpringMVC, that is the addition of a virtual directory named SpringMVC in the host name localhost, of course, here's the name entirely according to individual dependent.

注意:In the tomcat 5.5following
is not recommended server.xmlto configure, but in /conf/context.xmlindependent configuration. Because server.xmlit is not a dynamic resource re-loaded after server once started, to modify this file, you have to restart the server to reload. The context.xmldocument is not, tomcatthe server periodically to scan the document. Once a file has been modified (timestamp changed), it will automatically reload the file, without the need to restart the server

Increased 1.3 Web Hosting

In fact, this is to say the focus of this article, which is the beginning of this article talking about the kind of case. Each developing a website through all http://localhost/WebNameforms of access, is not too much trouble?

1.3.1 modify the hosts file, add the host name

  1. LinuxSystem hostsfiles/etc/hosts
  2. WindowsSystem hostsfiles C:\WINDOWS\system32\drivers\etc\hosts
    such as you want to add www.local.coma host, then add a line in this file: 127.0.0.1 www.local.comthat is when you visit www.local.comwhen, in fact, is to visit 127.0.0.1;
    if prefer short domain that can be added:127.0.0.1 SpringMVC

1.3.2 modify the configuration file

In </Host>adding a Host node and then:

<Host name="SpringMVC" appBase="/var/www/webapp/SpringMVC" unpackWARs="true" autoDeploy="false" 
xmlValidation="false" xmlNamespaceAware="false" />

That adds a named SpringMVCvirtual host, of course, may be name="www.local.com", this depending on the circumstances.
Thus, the visit IPis: http://SpringMVCa.

1.3.3 Directory Settings

Host SpringMVC's WEBfull file on the /var/www/webapp/SpringMVCdirectory, an increase in the catalog ROOTdirectory, add JavaEEthe site works, so you can http://SpringMVCaccess your Web site, and less behind /SpringMVCthe string in IPthe running to maintain a consistent environment.

1.4 Other issues

By http://SpringMVCaccessing a SpringMVCwebsite;
by http://localhostaccessing a Tomcatbuilt-in site;
the hostsfile already know, SpringMVCand localhostall corresponding IPis: 127.0.0.1
Well, by http://127.0.0.1accessing the website is that it?
In the Hostparent node is: <Engine name="Catalina" defaultHost="localhost">
As can be seen from the above, defaultHost="localhost"determines 127.0.0.1which site, if access is set SpringMVCto visitSpringMVC

1.5 server.xml file

Coming to an end this article, paste server.xmlthe file contents:

<?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="8080" 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" />
			<Host name="localhost" appBase="webapps" unpackWARs="true" 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>
			<Host name="SpringMVC" appBase="/var/www/webapp/SpringMVC"
				unpackWARs="true" autoDeploy="false" xmlValidation="false"
				xmlNamespaceAware="false" />
		</Engine>
	</Service>
</Server>
Published 334 original articles · won praise 186 · views 310 000 +

Guess you like

Origin blog.csdn.net/u012060033/article/details/103850591