只需配置DNS服务器,将每个主机名映射到正确的IP地址,然后配置nginx服务器,令其识别不同的主机名即可。这种虚拟主机技术,使很多虚拟主机可以共享同一个IP地址,有效解决了IP地址不足的问题。
nginx配置如下:
# 第一个虚拟主机
server {
listen 80;
server_name aaa.hbk.com;
access_log logs/aaa.access.log combined;
location / {
root /root/hbk/aaa;
index index.html;
}
}
# 第二个虚拟主机
server {
listen 80;
server_name bbb.hbk.com;
access_log logs/bbb.access.log combined;
location / {
root /root/hbk/bbb;
index index.html;
}
}
}
# 第二个虚拟主机
server {
listen 80;
server_name bbb.hbk.com;
access_log logs/bbb.access.log combined;
location / {
root /root/hbk/bbb;
index index.html;
}
}
# 第三个虚拟主机
server {
listen 80;
server_name ccc.hbk.com;
access_log logs/ccc.access.log combined;
location / {
root /root/hbk/ccc;
index index.html;
}
}
第一个虚拟主机响应aaa.hbk.com域名的请求
第二个虚拟主机响应bbb.hbk.com域名的请求
第三个虚拟主机响应ccc.hbk.com 域名的请求
[root@localhost hbk]# ll /root/hbk/{aaa,bbb,ccc}
/root/hbk/aaa:
总用量 4
-rwxrwxrwx 1 root root 25 8月 22 09:35 index.html
/root/hbk/bbb:
总用量 4
-rwxrwxrwx 1 root root 25 8月 22 09:36 index.html
/root/hbk/ccc:
总用量 4
-rwxrwxrwx 1 root root 36 8月 22 09:36 index.html
准备好测试页面
[root@localhost hbk]# cat /root/hbk/{aaa/index.html,bbb/index.html,ccc/index.html}
aaa.hbk.com huangbaokang
bbb.hbk.com huangbaokang
ccc.hbk.com huangbaokang
在本地测试电脑上配置hosts文件,windows系统的在C:\Windows\System32\drivers\etc\hosts文件,增加如下配置
127.0.0.1 aaa.hbk.com
127.0.0.1 bbb.hbk.com
127.0.0.1 ccc.hbk.com
重启nginx,浏览器验证:
优势,我在nginx上可以配置任意的域名,在需要请求该域名的本地主机上配置DNS服务器即可,避免了IP地址不够用的问题,如实验,我三个域名使用的是同一个IP。
还可以是复杂网络架构,如我本地主机可以给其他域名进行外网映射,(扩展你的想象)。