实验准备:
1、准备三个站点页面650) this.width=650;" src="https://s4.51cto.com/oss/201710/23/97ca07704a38c458a355afe1743fc5ee.png" title="2.png" alt="97ca07704a38c458a355afe1743fc5ee.png" />
2、准备三个IP地址
650) this.width=650;" src="https://s2.51cto.com/oss/201710/23/a44fe3cc04dcba1bff4c438d16a8bd77.png" title="3.png" alt="a44fe3cc04dcba1bff4c438d16a8bd77.png" />
一、实现基于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 8001listen 8002listen 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的虚拟主机
650) this.width=650;" src="https://s5.51cto.com/oss/201710/23/e69e296cc58cc055dfd13e2f79dfd443.png" title="20171019151042.png" alt="e69e296cc58cc055dfd13e2f79dfd443.png" />
当客户端访问一个网址的时候,会经过DNS服务器的解析将主机名解析成IP地址访问。但是对于一些访问量不大的网站,可能会出现多个网站放在一台web服务器上的情况,这时候,不管输入的是哪个网站地址,DNS解析到的都是一个IP地址,为了解决这种情况,就需要使用到虚拟主机。
实验步骤:
1、模拟DNS解析 vim /etc/hosts
[root@centOS6 app]# vim /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.35.136 www.a.com www.b.comwww.c.com
2、修改httpd服务的配置文件 /etc/httpd/conf/httpd.conf
NameVirtualHost *:80<virtualhost *:80>documentroot /app/site1servername www.a.comerrorlog logs/a.com.errlogcustomlog logs/a.com.accesslog combined</virtualhost> <virtualhost *:80>documentroot /app/site2servername www.b.comerrorlog logs/b.com.errlogcustomlog logs/b.com.accesslog combined</virtualhost> <virtualhost *:80>documentroot /app/site3servername www.c.comerrorlog logs/c.com.errlogcustomlog 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实现虚拟主机。
配置httpd服务虚拟主机
原文地址:http://13136984.blog.51cto.com/13126984/1975435