fdgre 发表于 2015-8-19 09:05:32

http实现虚拟主机的配置

   虚拟主机,简单的说指一个物理机服务多个站点,每个站点可通过一个或多个虚拟主机来实现
    http有3中类型的虚拟主机:
   (1)基于IP,配置时一定要配置好所以的IP

   (2)基于PORT,配置时一定要启用Listen PORT这一项,开启所有的监听端口

   (3)基于FQDN,配置时要启用NameVirtualHost这一项



相关配置

配置前提:
    已经安装好httpd服务器


配置步骤:

1.      基于端口的虚拟主机配置


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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
# cd /vhosts/a.com/htdocs/
# vimindex.html
         <h1>a.com</h1>
#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
# 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
                                                         




到这里我们基于端口的虚拟主机就配置完成,现在在浏览器中输入:http://192.168.1.11:8080/http://192.168.1.11:80/观察测试结果
2.      基于FQDN的虚拟主机的配置
编辑配置文件,具体内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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>

# mkdir/vhosts/c.net/htdocs -p
#vim /vhosts/c.net/htdocs/index.html
         <h1>c.net</h1>

# 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的虚拟主机配置完毕,下面进行访问测试:# curlhttp://www.c.net<h1>c.net</h1># curlhttp://www.b.org<h1>b.org</h1># curlhttp://www.a.com<h1>a.com</h1>

[*]3.      基于IP的虚拟主机配置


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifconfig eth0:0 192.168.1.21
# 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>
# httpd-t
httpd: Could not reliablydetermine the server's fully qualified domain name, using 172.16.33.1 forServerName
Syntax OK
# !s
service httpd restart
Stopping httpd:                                          
Starting httpd: httpd:Could not reliably determine the server's fully qualified domain name, using172.16.33.1 for ServerName
                                                         





到这里我们基于IP的虚拟主机就配置完了,在浏览器中测试输入:http://192.168.1.199/http://192.168.1.101/

[*]4.      基于IP+PORT的虚拟主机的配置



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
编辑配置文件/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测试,观察结果

页: [1]
查看完整版本: http实现虚拟主机的配置