nginx配置基于域名的虚拟主机

只需配置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。
还可以是复杂网络架构,如我本地主机可以给其他域名进行外网映射,(扩展你的想象)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值