1. 程式人生 > >使用Tomcat建立基於主機名的虛擬主機

使用Tomcat建立基於主機名的虛擬主機

使用主機名的方式建立多個虛擬主機的演示步驟。

  1. 修改Tomcat下的Server.xml檔案,在其中加入如下內容
    <Host name="site1" debug="0" appBase="d:\VirtualHost1">
        <Context path="" docBase="." debug="0"/>
    </Host>
    <Host name="site2" debug="0" appBase="d:\VirtualHost2">
        <Context path="" docBase="." debug
="0"/>
</Host>

上述程式碼的意思是新建了兩個WEB站點,主機名分別為site1和site2,根目錄對應的本地檔案系統目錄分別為d:\VirtualHost1和d:\VirtualHost2
2. 在d:盤下建立名為VirtualHost1和VirtualHost2兩個目錄,並在這兩個目錄中分別建立一個test.html的檔案,在d:\VirtualHost1\test.html中寫入:
這是d:\VirtualHost1目錄中的test.html檔案
在d:\VirtualHost2\test.html中寫入:
這是d:\VirtualHost2目錄中的test.html檔案


3. 儲存修改的Server.xml檔案,重新啟動Tomcat。
4. 開啟C:\Windows\System32\drivers\etc\hosts檔案,在檔案中127.0.0.1 localhost後面新增以下兩行:
*127.0.0.1 site1
127.0.0.1 site2
*
5. 儲存檔案,在瀏覽器中輸入http://site1:8080/test.html會得到如下結果:
這裡寫圖片描述
在瀏覽器中輸入http://site2:8080/test.html會得到如下結果:
這裡寫圖片描述
6. 可見,用主機名的方式在同一臺WEB伺服器上建立多個虛擬主機後,在WEB瀏覽器中使用主機名訪問Web伺服器時,Web伺服器將選擇與該主機名關聯的WEB站點進行響應。