基于ip的多虚拟主机
在同一台服务器上配置多个ip
[httpd_server]# ip address add 192.168.43.100/24 dev ens33
[httpd_server]# ip address add 192.168.43.101/24 dev ens33
[httpd_server]# ip address add 192.168.43.102/24 dev ens33
建立多个站点
[httpd_server]# mkdir /data/{a,b,c}site -pv
[httpd_server]# echo /data/asite/a > /data/asite/index.html
[httpd_server]# echo /data/bsite/b > /data/bsite/index.html
[httpd_server]# echo /data/csite/c > /data/csite/index.html
配置文件
[httpd_server]# vim /etc/httpd/conf.d/a.conf
<virtualhost 192.168.43.100:80>
documentroot "/data/asite"
<directory "/data/asite">
require all granted
</directory>
</virtualhost>
[httpd_server]# vim /etc/httpd/conf.d/b.conf
<virtualhost 192.168.43.101:80>
documentroot "/data/bsite"
<directory "/data/bsite">
require all granted
</directory>
</virtualhost>
[httpd_server]# vim /etc/httpd/conf.d/c.conf
<virtualhost 192.168.43.102:80>
documentroot "/data/csite"
<directory "/data/csite">
require all granted
</directory>
</virtualhost>
启动服务
[httpd_server]# httpd -t
[httpd_server]# systemctl start httpd
客户端访问
[client]# curl 192.168.43.100
/data/asite/a
[client]# curl 192.168.43.101
/data/bsite/b
[client]# curl 192.168.43.102
/data/csite/c
基于 port 的多虚拟主机
配置文件
[httpd_server]# vim /etc/httpd/conf.d/a.conf
listen 81
<virtualhost *:81>
documentroot "/data/asite"
<directory "/data/asite">
require all granted
</directory>
</virtualhost>
[httpd_server]# vim /etc/httpd/conf.d/b.conf
listen 82
<virtualhost *:82>
documentroot "/data/bsite"
<directory "/data/bsite">
require all granted
</directory>
</virtualhost>
[httpd_server]# vim /etc/httpd/conf.d/c.conf
listen 83
<virtualhost *:83>
documentroot "/data/csite"
<directory "/data/csite">
require all granted
</directory>
</virtualhost>
客户端访问
[client]# curl 192.168.43.17:81
/data/asite/a
[client]# curl 192.168.43.17:82
/data/bsite/b
[client]# curl 192.168.43.17:83
/data/csite/c
基于主机头的虚拟主机
配置文件
[httpd_server]# vim /etc/httpd/conf.d/a.conf
<virtualhost *:80>
documentroot "/data/asite"
servername www.a.com
<directory "/data/asite">
require all granted
</directory>
</virtualhost>
[httpd_server]# vim /etc/httpd/conf.d/b.conf
<virtualhost *:80>
documentroot "/data/bsite"
servername www.b.com
<directory "/data/bsite">
require all granted
</directory>
</virtualhost>
[httpd_server]# vim /etc/httpd/conf.d/c.conf
<virtualhost *:80>
documentroot "/data/csite"
servername www.c.com
<directory "/data/csite">
require all granted
</directory>
</virtualhost>
客户端访问
客户端访问服务器的域名,需要dns服务器,由于没有配置dns服务器,所以在客户端这边配置hosts文件
[client]# vim /etc/hosts
192.168.43.17 www.a.com www.b.com www.c.com
[client]# curl www.a.com
/data/asite/a
[client]# curl www.b.com
/data/bsite/b
[client]# curl www.c.com
/data/csite/c
telnet 模拟浏览器访问httpd服务器
[client]# telnet www.a.com 80
Trying 192.168.43.17...
Connected to www.a.com.
Escape character is '^]'.
GET / HTTP/1.1 // 自己写的
host: www.b.com // 自己写的
HTTP/1.1 200 OK
Date: Thu, 30 Jan 2020 05:04:20 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 30 Jan 2020 04:05:25 GMT
ETag: "e-59d53910f312f"
Accept-Ranges: bytes
Content-Length: 14
Content-Type: text/html; charset=UTF-8
/data/bsite/b //返回的内容
GET / Connection closed by foreign host.