Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

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>