httpd服务有一个重要的功能,就是虚拟主机的实现。我们可以通过不同的方式实现HTTP的虚拟主机。
虚拟主机的实现有三种方法:
基于ip:端口为默认端口,ip地址不同
基于端口:IP地址相同,端口号不同
基于FQDN全称域名
有三种实现方案:
基于ip:为每个虚拟主机准备至少一个ip地址
基于port:为每个虚拟主机使用至少一个独立的port
基于FQDN:为每个虚拟主机使用至少一个FQDN
注意:一般虚拟机不要与main主机混用;因此,要使用虚拟主机,一般先禁用main主机
禁用方法:注释中心主机的DocumentRoot指令即可
实验环境:
centos7.4 http2.4
第一种实现方案,基于不同的IP实现:
1、先在指定目录准备相关文件。
[root@centos7 ~]# cd /var/www/html/
[root@centos7 html]# mkdir -v site{a,b,c}
mkdir: 已创建目录 "sitea"
mkdir: 已创建目录 "siteb"
mkdir: 已创建目录 "sitec"
[root@centos7 html]# mkdir site{a,b,c}/logs //创建日志存放目录
[root@centos7 html]# ls
index.html sitea siteb sitec
[root@centos7 html]# echo /var/www/html/sitea/sitea.html > /var/www/html/sitea/index.html
[root@centos7 html]# echo /var/www/html/siteb/siteb.html > /var/www/html/siteb/index.html
[root@centos7 html]# echo /var/www/html/sitec/sitec.html > /var/www/html/sitec/index.html
[root@centos7 html]# tree
.
├── index.html
├── sitea
│ ├── index.html
│ └── logs
├── siteb
│ ├── index.html
│ └── logs
└── sitec
├── index.html
└── logs
2、为了方便管理,我们在/etc/httpd/conf.d/目录下创建ip_vhost.conf子配置文件
[root@centos7 conf.d]# vim /etc/httpd/conf.d/ip_vhost.conf
<Directory /var/www/html/>
require all granted
</Directory>
<VirtualHost 192.168.239.11:80>
DocumentRoot "/var/www/html/sitea"
ServerName www.a.com
Errorlog "logs/a.com_error_log"
TransferLog "logs/a.com_access_log"
</VirtualHost>
<VirtualHost 192.168.239.22:80>
DocumentRoot "/var/www/html/siteb"
ServerName www.b.com
Errorlog "logs/b.com_error_log"
TransferLog "logs/b.com_access_log"
</VirtualHost>
<VirtualHost 192.168.239.33:80>
DocumentRoot "/var/www/html/sitec"
ServerName www.c.com
Errorlog "logs/c.com_error_log"
TransferLog "logs/c.com_access_log"
</VirtualHost>
检查语法有没有问题然后重启服务
[root@centos7 conf.d]# httpd -t
Syntax OK
[root@centos7 conf.d]# systemctl restart httpd
当然,我们要实现多个IP的虚拟主机就得在网卡上添加多个IP地址。
[root@centos7 ~]# ip addr add 192.168.239.11/24 dev ens33
[root@centos7 ~]# ip addr add 192.168.239.22/24 dev ens33
[root@centos7 ~]# ip addr add 192.168.239.33/24 dev ens33
[root@centos7 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:60:45:37 brd ff:ff:ff:ff:ff:ff
inet 192.168.239.128/24 brd 192.168.239.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.239.11/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet 192.168.239.22/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet 192.168.239.33/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe60:4537/64 scope link
valid_lft forever preferred_lft forever
3、在浏览器上输入相应的IP地址
第二种实现方案,基于不同端口实现
1、基于端口的虚拟主机是IP地址相同,只是监听端口不同,这里可以自定义端口号,需要在配置文件中首先声明监听这些端口,然后在进行配置。在/etc/httpd/conf.d/目录下创建port_vhost.conf子配置文件。(避免冲突先把之前子配置文件移走)
[root@centos7 conf.d]# vim port_vhost.conf
listen 81
listen 82
listen 83
<Directory /var/www/html/>
require all granted
</Directory>
<VirtualHost *:81>
DocumentRoot "/var/www/html/sitea"
ServerName www.a.com
Errorlog "logs/a.com_error_log"
TransferLog "logs/a.com_access_log"
</VirtualHost>
<VirtualHost *:82>
DocumentRoot "/var/www/html/siteb"
ServerName www.b.com
Errorlog "logs/b.com_error_log"
TransferLog "logs/b.com_access_log"
</VirtualHost>
<VirtualHost *:83>
DocumentRoot "/var/www/html/sitec"
ServerName www.c.com
Errorlog "logs/c.com_error_log"
TransferLog "logs/c.com_access_log"
</VirtualHost>
检查语法有没有问题然后重启服务
[root@centos7 conf.d]# httpd -t
Syntax OK
[root@centos7 conf.d]# systemctl restart httpd
2、打开浏览器访问在ip地址相同的情况下不同端口的主页面,得到三个不同页面.
第三种实现方案,基于不同的FQDN
1、基于FQDN(域名)的实现方式是比较常用的,当用户输入不同的域名时就会访问到不同的站点。同样跟上面的步骤一样,在/etc/httpd/conf.d/目录下创建fqdn_vhost.conf子配置文件。(避免冲突先把之前子配置文件移走),在httpd2.2(centos6的默认版本)中得在配置文件中加上NameVirtualHost *:80这一行才能生效。
[root@centos7 conf.d]# vim fqdn_vhost.conf
<Directory /var/www/html/>
require all granted
</Directory>
<VirtualHost *:80>
DocumentRoot "/var/www/html/sitea"
ServerName www.a.com
ErrorLog "logs/a.com_error_log"
TransferLog "logs/a.com_access_log"
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/siteb"
ServerName www.b.com
ErrorLog "logs/b.com_error_log"
TransferLog "logs/b.com_access_log"
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/sitec"
ServerName www.c.com
ErrorLog "logs/c.com_error_log"
TransferLog "logs/c.com_access_log"
</VirtualHost>
检查语法有没有问题然后重启服务
[root@centos7 conf.d]# httpd -t
Syntax OK
[root@centos7 conf.d]# systemctl restart httpd
2、在测试机的/etc/hosts文件加上http服务器的地址及域名,用于域名解析(由于这里没有待见DNS服务器的原因,才这么做)
[root@centos6 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.239.128 www.a.com www.b.com www.c.com //加上这行
3、使用curl工具进行测试
[root@centos6 ~]# curl http://www.a.com
/var/www/html/sitea/sitea.html
[root@centos6 ~]# curl http://www.b.com
/var/www/html/siteb/siteb.html
[root@centos6 ~]# curl http://www.c.com
/var/www/html/sitec/sitec.html
还可以加上-v选项查看详细信息,以www.a.com为例
[root@centos6 ~]# curl -v www.a.com
* About to connect() to www.a.com port 80 (#0)
* Trying 192.168.239.128... connected
* Connected to www.a.com (192.168.239.128) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.a.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Wed, 31 Jul 2019 16:24:09 GMT
< Server: Apache
< Last-Modified: Tue, 30 Jul 2019 15:43:17 GMT
< ETag: "1f-58ee7de0ba23c"
< Accept-Ranges: bytes
< Content-Length: 31
< Content-Type: text/html; charset=UTF-8
<
/var/www/html/sitea/sitea.html
* Connection #0 to host www.a.com left intact
* Closing connection #0