虛擬主機的介紹html
虛擬主機,簡單的說指一個物理機服務多個站點,每一個站點可經過一個或多個虛擬主機來實現node
http有3中類型的虛擬主機:vim
(1)基於IP,配置時必定要配置好因此的IP
瀏覽器
(2)基於PORT,配置時必定要啓用Listen PORT這一項,開啓全部的監聽端口
bash
(3)基於FQDN,配置時要啓用NameVirtualHost這一項
服務器
相關配置dom
配置前提:curl
已經安裝好httpd服務器
ide
配置步驟:測試
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 ]
到這裏咱們基於端口的虛擬主機就配置完成,如今在瀏覽器中輸入:
觀察測試結果
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>
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的虛擬主機就配置完了,在瀏覽器中測試輸入:
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:80,192.168.1.101:8080測試,觀察結果