分类: Java
2015-01-22 11:51:55
tomcat 7的xml配置文件server.xml通常在默认情况下,在
1 |
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">Host> |
· 最简的server.xml
1 2 3 4 |
<Engine name="Catalina" defaultHost="ren"> <Host name="ren" appBase="renapps"/> <Host name="stimpy" appBase="stimpyapps"/> Engine> |
·
Host就是虚拟主机的元素(标签)
默认虚拟主机的应用程序目录是指向tomcat目录下的webapps这个文件夹的,默认打开的应用是ROOT,即在浏览器中输入打开的是ROOT这个Web应用。
在第一个虚拟主机的结束标签后面添加:
1 2 3 |
<Host name="" appBase="/var/tomcat/apps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/var/tomcat/apps/test" path="" reloadable="true" /> Host> |
Host标签里面多出的属性先不用管它,有与没有都不会影响后面的操作。
appBase设置成了docBase的上一级目录,在apps目录下的test文件夹才是存放web应用的地方。
docBase目录下放一个index.html文件,内容为"Hello,成功了!"。
配置完成后,重启tomcat使用配置生效。
在完成域名绑定、或修改hosts文件后,即可访问: 站点。
· Host Name Aliases
· Remote Address Filter
The Remote Address Filter allows you to compare the IP address of the client that submitted this request against one or more regular expressions, and either allow the request to continue or refuse to process the request from this client. A Remote Address Filter can be associated with any Catalina container (Engine, Host, or Context), and must accept any request presented to this container for processing before it will be passed on.
To allow access only for the clients connecting from localhost:
原文:http://ritcher.iteye.com/blog/349972
一般情况下,tomcat只能配置基于域名和IP的虚拟主机,只需要在server.xml里面增加host即可,举例:
1 2 3 4 5 |
<Host name="" debug="0" appBase="e:\VSB2006\owners\" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="_log." suffix=".txt" timestamp="true" /> Host> |
Tomcat的文档中和网上都无法找到基于端口的虚拟主机的配法,经过尝试,发现了一个方法可以达到这个效果。
我们看一个最精简的server.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<Server port="8005" shutdown="SHUTDOWN">
….省略
<Service name="Catalina">
<Connector port="8080" /> <Connector port="8009" protocol="AJP/1.3" />
<Engine name="Catalina" defaultHost="localhost"> <Logger className="org.apache.catalina.logger.FileLogger" /> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" /> <Host name="localhost" appBase="webapps" /> Engine>
Service>
Server> |
虽然可以增加Connector来增加服务的端口,但是host中没有提供端口的选项,这样增加出来的端口和源端口是一样的虚拟主机设定,无法达到我们的要求。
单纯增加端口没有办法,那么我们可以增加service,举例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<Server port="8005" shutdown="SHUTDOWN">
….省略
<Service name="Catalina"> <Connector port="8080" /> <Connector port="8009" protocol="AJP/1.3" /> <Engine name="Catalina" defaultHost="localhost"> <Logger className="org.apache.catalina.logger.FileLogger" /> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" /> <Host name="localhost" appBase="webapps" /> Engine> Service>
<Service name="Catalina2"> <Connector port="8090" /> <Engine name="Catalina2" defaultHost="localhost"> <Logger className="org.apache.catalina.logger.FileLogger" /> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" /> <Host name="localhost" appBase="webapps2" /> Engine> Service>
Server> |
增加的service可以分配自己的connector和host,只要和主service不冲突即可
通过这个办法,可以让vsb2006的管理端运行在一个端口上,而站点运行在另外的端口上,完全不冲突
经实验此方式可行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Service name="Timaweb"> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" /> <Engine name="Timaweb" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> Realm> <Host name="localhost" appBase="Timaweb" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/tima" prefix="Timaweb_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b %{Referer}i %{User-Agent}i %D" />
<Context path="/gaei/ev" docBase="b2c-admin" debug="0"/> Host> Engine> Service> |