首页 > Web开发 > 详细

3种方法实现http虚拟主机

时间:2019-05-14 00:23:14      阅读:153      评论:0      收藏:0      [点我收藏+]
http访问

站点标识: socket

IP相同,但端口不同
IP不同,但端口均为默认端口
FQDN不同:请求报文中首部 Host: www.magedu.com


有三种实现方案:

基于ip:为每个虚拟主机准备至少一个ip地址
基于port:为每个虚拟主机使用至少一个独立的port
基于FQDN:为每个虚拟主机使用至少一个FQDN

1基于PORT的多虚拟主机

  • 建立相应目录及index文件

技术分享图片

#vim /etc/httpd/conf.d/vhosts.conf 
listen 8001
listen 8002
listen 8003

<virtualhost *:8001>
documentroot /data/site1
<directory /data/site1>
require all granted
</directory>
</virtualhost>

<virtualhost *:8002>
<directory /data/site2>                                                                                               
require all granted
</directory>
documentroot /data/site2
</virtualhost>

<virtualhost *:8003>
<directory /data/site3>                                                                                               
require all granted
</directory>
documentroot /data/site3
</virtualhost>
  • 测试
    root:~ # curl 192.168.64.150:8001
    111
    root:~ # curl 192.168.64.150:8002
    222

    实验:基于ip的多虚拟主机

  • 添加相应ip
ip a a 192.168.36.7 dev ens33
ip a a 192.168.36.8 dev ens33
ip a a 192.168.36.9 dev ens33
vim /etc/httpd/conf.d/vhosts.conf 
<virtualhost 192.168.36.7:80>
documentroot /data/site1
<directory /data/site1>
require all granted
</directory>
</virtualhost>

<virtualhost 192.168.36.8:80>
<directory /data/site2>
require all granted
</directory>
documentroot /data/site2
</virtualhost>

<virtualhost 192.168.36.9:80>                                                                                         
<directory /data/site3>
require all granted
</directory>
documentroot /data/site3
</virtualhost>
  • 测试
    root:~ # curl 192.168.36.6
    111
    root:~ # curl 192.168.36.7
    222

实验:基于主机头的多虚拟主机

vim /etc/httpd/conf.d/vhosts.conf 
<virtualhost *:80>
documentroot /data/site1
servername www.a.com
<directory /data/site1>
require all granted
</directory>
CustomLog "logs/a_access_log" combined
</virtualhost>

<virtualhost *:80>
<directory /data/site2>                                                                                               
require all granted
</directory>
documentroot /data/site2
servername www.b.com
CustomLog "logs/b_access_log" combined
</virtualhost>

<virtualhost *:80>
servername www.c.com
<directory /data/site3>                                                                                               
require all granted
</directory>
documentroot /data/site3
CustomLog "logs/c_access_log" combined
</virtualhost>
  • 在测试机上
在windows编辑  C:\Windows\System32\drivers\etc\hosts #吧只读去掉
192.168.36.7 www.a.com www.b.com www.c.com

技术分享图片

3种方法实现http虚拟主机

原文:https://blog.51cto.com/9019400/2393436

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!