實驗準備:
2、準備三個IP地址
一、實現基於ip的虛擬主機
1、修改httpd服務的配置文件 /etc/httpd/conf/httpd.conf
<virtualhost 192.168.35.13:80> documentroot /app/site1 </virtualhost> <virtualhost 192.168.35.14:80> documentroot /app/site2 </virtualhost> <virtualhost 192.168.35.15:80> documentroot /app/site3 </virtualhost>
2、測試連接
[root@localhost ~]# curl 192.168.35.13 /app/site1/index.html [root@localhost ~]# curl 192.168.35.14 /app/site2/index.html [root@localhost ~]# curl 192.168.35.15 /app/site3/index.html
二、基於端口不同實現虛擬主機
實驗步驟:
1、修改httpd服務的配置文件 /etc/httpd/conf/httpd.conf
listen 8001 listen 8002 listen 8003 <virtualhost *:8001> documentroot /app/site1 </virtualhost> <virtualhost *:8002> documentroot /app/site2 </virtualhost> <virtualhost *:8003> documentroot /app/site3 </virtualhost>
2、測試連接
[root@localhost ~]# curl192.168.35.136:8001 /app/site1/index.html [root@localhost ~]# curl192.168.35.136:8002 /app/site2/index.html [root@localhost ~]# curl 192.168.35.136:8003 /app/site3/index.html
三、實現基於FQDN的虛擬主機
當客戶端訪問一個網址的時候,會經過DNS服務器的解析將主機名解析成IP地址訪問。但是對於一些訪問量不大的網站,可能會出現多個網站放在一臺web服務器上的情況,這時候,不管輸入的是哪個網站地址,DNS解析到的都是一個IP地址,爲了解決這種情況,就需要使用到虛擬主機。
實驗步驟:
1、模擬DNS解析 vim /etc/hosts
[root@centOS6 app]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.35.136 www.a.com www.b.comwww.c.com
2、修改httpd服務的配置文件 /etc/httpd/conf/httpd.conf
NameVirtualHost *:80 <virtualhost *:80> documentroot /app/site1 servername www.a.com errorlog logs/a.com.errlog customlog logs/a.com.accesslog combined </virtualhost> <virtualhost *:80> documentroot /app/site2 servername www.b.com errorlog logs/b.com.errlog customlog logs/b.com.accesslog combined </virtualhost> <virtualhost *:80> documentroot /app/site3 servername www.c.com errorlog logs/c.com.errlog customlog logs/c.com.accesslog combined </virtualhost>
3、測試連接
[root@localhost ~]# curl www.a.com /app/site1/index.html [root@localhost ~]# curl www.b.com /app/site2/index.html [root@localhost ~]# curl www.c.com /app/site3/index.html
實驗小結:基於ip地址實現虛擬主機,有多少個網址就需要多少個IP地址,這樣就造成了資源的浪費,也增加了成本,而基於端口在訪問的時候需要輸入端口號,而一般用戶並都能記住端口號,所以訪問起來比較麻煩,所以目前的主流是局域FQDN實現虛擬主機。