1. 程式人生 > 實用技巧 >Tomcat6 只允許指定域名訪問,禁用IP地址訪問,防止惡意解析

Tomcat6 只允許指定域名訪問,禁用IP地址訪問,防止惡意解析

 1 運維網監控突然同事反應,在百度上搜索其他域名,竟然打開了和我們P2P一模一樣的網站,我第一個反應是原始碼被盜用了。後來發現,是域名被惡意解析了,解決方法 1、禁止IP地址訪問專案  2、只允許指定的域名訪問。
 2 
 3 環境:tomcat 6
 4 方法: 修改tomcat 6 的配置檔案  tomcat/conf/server.xml,實現原理,將tomcat 預設引數defaultHost指向一個不存在的域名上,並新增同樣的虛擬目錄,這樣當被一個未知的域名解析過來後,訪問的預設虛擬目錄,但這個目錄中並沒有任何專案,這樣就達到效果了。
 5 以下是我配置檔案引數 只貼出 Engine 之間的哦~
 6 
 7 
 8 
 9 <Engine name="Catalina" defaultHost="192.168.1.1">   <!-- 預設引數我設定的是我伺服器的外網IP地址    -->
10       <!--For clustering, please take a look at documentation at:
11           /docs/cluster-howto.html  (simple how to)
12           /docs/config/cluster.html (reference documentation) -->
13       <!--
14       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
15       -->      
16       <!-- The request dumper valve dumps useful debugging information about
17            the request and response data received and sent by Tomcat.
18            Documentation at: /docs/config/valve.html -->
19       <!--
20       <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
21       -->
22       <!-- This Realm uses the UserDatabase configured in the global JNDI
23            resources under the key "UserDatabase".  Any edits
24            that are performed against this UserDatabase are immediately
25            available for use by the Realm.  -->
26       <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
27              resourceName="UserDatabase"/>
28       <!-- Define the default virtual host
29            Note: XML Schema validation will not work with Xerces 2.2.
30        -->
31       <Host name=www.abc.com  appBase="webapps" <!-- 這個虛擬目錄是你真實使用的虛擬目錄,域名也是你指定的域名 -->
32          unpackWARs="true" autoDeploy="true"
33          xmlValidation="false" xmlNamespaceAware="false">
34       <Context path="" docBase="/home/web/apache-tomcat-6.0.39/webapps/abc" debug="0" reloadable="true"/> <!-- 指定虛擬目錄的專案地址 -->
35       </Host>
36    
37       <Host name="192.168.1.1"  appBase="ipapps"   <!-- 設定你伺服器的外網IP地址,並建立一個虛假的虛擬目錄  ipaaps  這個是不存在的,當訪問IP地址或其他域名,將被轉向到訪問這個虛擬目錄上 -->
38           unpackWARs="true" autoDeploy="true"
39           xmlValidation="false" xmlNamespaceAware="false">
40       </Host>
41    
42 </Engine>