http實現虛擬主機的配置

虛擬主機的介紹

    虛擬主機,簡單的說指一個物理機服務多個站點,每個站點可通過一個或多個虛擬主機來實現


    http有3中類型的虛擬主機:

     (1)基於IP,配置時一定要配置好所以的IP

     (2)基於PORT,配置時一定要啓用Listen PORT這一項,開啓所有的監聽端口

     (3)基於FQDN,配置時要啓用NameVirtualHost這一項



相關配置


配置前提:

    已經安裝好httpd服務器


配置步驟:


1.      基於端口的虛擬主機配置

 

#vim/etc/httpd/conf/httpd.conf
…..添加如下內容………..
Listen 8080
………………….
<VirtualHost *:80>
ServerName www.a.com
DocumentRoot/vhosts/a.com/htdocs/
</VirtualHost>
 
<VirtualHost *:8080>
ServerName www.b.org
DocumentRoot/vhosts/b.org/htdocs
</VirtualHost>
 
# mkdir/vhosts/{a.com,b.org}/htdocs –pv
[root@node1 ~]# cd /vhosts/a.com/htdocs/
[root@node1 htdocs]# vimindex.html
         <h1>a.com</h1>
[root@node1 htdocs]#vim /vhosts/b.org/htdocs/index.html
           <h1>b.org</h1>
#vim /etc/hosts
…………添加
192.168.1.11 www.a.com
192.168.1.12 www.b.org
#httpd –t
[root@node1 htdocs]# service httpd restart
Stopping httpd:                                            [ OK  ]
Starting httpd: httpd:Could not reliably determine the server's fully qualified domain name, using172.16.33.1 for ServerName
                                                          [  OK  ]

到這裏我們基於端口的虛擬主機就配置完成,現在在瀏覽器中輸入:

http://192.168.1.11:8080/

http://192.168.1.11:80/

觀察測試結果


2.      基於FQDN的虛擬主機的配置

編輯配置文件,具體內容如下:

NameVirtualHost 192.168.1.11:80   #啓用NameVirtualHost 這一項
……….
<VirtualHost192.168.1.11:80>
        ServerName www.a.com
        DocumentRoot /vhosts/a.com/htdocs/
</VirtualHost>
 
<VirtualHost192.168.1.11:80>
        ServerName www.b.org
        DocumentRoot /vhosts/b.org/htdocs
</VirtualHost>
<VirtualHost192.168.1.11:80>
        ServerName www.c.net
        DocumentRoot /vhosts/c.net/htdocs
</VirtualHost>
 
[root@node1 htdocs]# mkdir/vhosts/c.net/htdocs -p
[root@node1 htdocs]#vim /vhosts/c.net/htdocs/index.html
           <h1>c.net</h1>
 
[root@node1 vhosts]# vim/etc/hosts
……添加…..
192.168.1.11 www.a.com
192.168.1.11 www.b.org
192.168.1.11 www.c.net

 

基於FQDN的虛擬主機配置完畢,下面進行訪問測試:

[root@node1 vhosts]# curlhttp://www.c.net

<h1>c.net</h1>

[root@node1 vhosts]# curlhttp://www.b.org

<h1>b.org</h1>

[root@node1 vhosts]# curlhttp://www.a.com

<h1>a.com</h1>

 

  1. 3.      基於IP的虛擬主機配置

[root@node1 vhosts]#ifconfig eth0:0 192.168.1.21
[root@node1 vhosts]# vim/etc/httpd/conf/httpd.conf
………..
<VirtualHost192.168.1.11:80>
        ServerName www.a.com
        DocumentRoot /vhosts/a.com/htdocs/
</VirtualHost>
<VirtualHost192.168.1.21:80>
        ServerName www.b.org
        DocumentRoot /vhosts/b.org/htdocs/
</VirtualHost>
[root@node1 vhosts]# httpd-t
httpd: Could not reliablydetermine the server's fully qualified domain name, using 172.16.33.1 forServerName
Syntax OK
[root@node1 vhosts]# !s
service httpd restart
Stopping httpd:                                           [  OK  ]
Starting httpd: httpd:Could not reliably determine the server's fully qualified domain name, using172.16.33.1 for ServerName
                                                          [  OK  ]

        

到這裏我們基於IP的虛擬主機就配置完了,在瀏覽器中測試輸入:

http://192.168.1.199/

http://192.168.1.101/

 

  1. 4.      基於IP+PORT的虛擬主機的配置

   

編輯配置文件/etc/httpd/conf/httpd.conf:
    Listen 8080  #添加這一項
   ……………..
      <VirtualHost192.168.1.11:80>
        ServerName www.a.com
        DocumentRoot /vhosts/a.com/htdocs/
</VirtualHost>
<VirtualHost 192.168.1.21:80>
        ServerName www.b.org
        DocumentRoot /vhosts/b.org/htdocs/
</VirtualHost>
 
<VirtualHost192.168.1.11:8080>
        ServerName www.c.net
        DocumentRoot /vhosts/c.net/htdocs/
</VirtualHost>
 
#httpd -t
#service httpd restart


到這裏我們基於IP+PORT的虛擬主機就配置完成了,在瀏覽器中訪問192.168.1.199:80,192.168.1.101:80192.168.1.101:8080測試,觀察結果

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章