Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

2.3. 虚拟主机配置

[注意]注意

Tomcat 8 取消了 xmlValidation="false" xmlNamespaceAware="false" 两个配置项。

appBase 是防止 war 文件的扫描目录。

2.3.1. 方案一

将配置写入server.xml文件

			
      <Host name="www.example.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
            <Context path="" docBase="/www/example.com/www.example.comm" debug="0" reloadable="false"/>
      </Host>
      <Host name="news.example.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
            <Context path="" docBase="/www/example/news.example.com" debug="0" reloadable="false"/>
      </Host>
			
			

2.3.2. 方案二

在 $CATALINA_HOME/conf/Catalina/ 下创建配置文件

			
vim server.xml

<Engine name="Catalina" defaultHost="neo">
    <Host name="neo"    appBase="webapps"/>
    <Host name="other" appBase="webapps"/>
</Engine>
			
			

Webapps Directory

			
mkdir $CATALINA_HOME/conf/Catalina/neo
			
			

Configuring Your Contexts

			
mkdir $CATALINA_HOME/conf/Catalina/neo

cp $CATALINA_HOME/conf/Catalina/localhost/manager.xml $CATALINA_HOME/conf/Catalina/neo/ROOT.xml

or

cp $CATALINA_HOME/conf/Catalina/localhost/manager.xml $CATALINA_HOME/conf/Catalina/neo
			
			

2.3.3. Alias 别名

别名的功能是为虚拟主机绑定多个域名

			
<Host name="www.example.com" debug="9" appBase="webapps"
                              unpackWARs="false" autoDeploy="false"
                               xmlValidation="false" xmlNamespaceAware="false">
	<Alias>www.example.net</Alias>
	<Alias>exmaple.com</Alias>
	<Alias>224.25.22.70</Alias>
</Host>

			
			

2.3.4. access_log

			
<Host name="localhost" ...>
  ...
  <Valve className="org.apache.catalina.valves.AccessLogValve"
         prefix="localhost_access_log." suffix=".txt"
         pattern="common"/>
  ...
</Host>
			
			
			
	<Valve className"org.apache.catalina.valves.AccessLogValve"
		directory="logs/access"
		prefix="www.netkiller.cn.access"
		suffix=".log"
		pattern="%{X-Forwarded-FOR}i %a %v %U %t %m %s  %{User-Agent}i" resolveHosts="false"/>
			
			

2.3.5. Context 配置

			
	<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
		<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>
		<Context docBase="Struts" path="/Struts" reloadable="true" source="org.eclipse.jst.jee.server:Struts"/>
	</Host>
			
			

docBase如果是绝对路径就会忽略appBase="webapps"的设置。

			
<Context path="" docBase="/www/example.com/www.example.com/WebContent"  reloadable="false">
			
			

appBase + docBase 方案

			
<Host name="localhost"  appBase="/www/example.com" unpackWARs="true" autoDeploy="true">
	<Alias>www.example.com</Alias>

	<Context path="" docBase="www.example.com"  reloadable="false"></Context>

</Host>			
			
			

2.3.6. 主机绑定IP地址

			
    <Host name="223.225.22.72"  appBase="/www/netkiller.cn" unpackWARs="true" autoDeploy="true">
		<Alias>www.netkiller.cn</Alias>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="www.netkiller.cn_access_log." suffix=".log"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
		<Logger className="org.apache.catalina.logger.FileLogger" 
                 directory="logs"  prefix="web_log." suffix=".txt"  timestamp="true"/>
        <Context path="" docBase="www.netkiller.cn"  reloadable="true"></Context>
        
	</Host>
	<Host name="223.225.22.73"  appBase="/www/netkiller.cn" unpackWARs="true" autoDeploy="true">
		<Alias>admin.netkiller.cn</Alias>
		<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
	               prefix="admin.netkiller.cn_access_log." suffix=".log"
        	       pattern="%h %l %u %t &quot;%r&quot; %s %b" />
		<Context path="" docBase="admin.netkiller.cn"  reloadable="true" />
	</Host>