nginx系列-03-虚拟主机的配置

* 以下所有的测试都是在CentOS6系统上进行的 *

* 另外,本篇文章只配置最基本的配置项。其他配置可参考本人该系列的其他文章 *

1 什么是虚拟主机?都有哪些类型的虚拟主机?

虚拟主机当然是虚拟的主机而非真正意义上的物理主机。
他是利用软硬件技术,将一台真正的物理主机分成一台台虚拟主机。
在”浏览器”看来,每台虚拟主机和真正的物理主机没什么区别。这里所说的”浏览器”指的是广义上的一切访问者。

虚拟主机从实现的技术不同可以分为以下三类:

  • 基于IP的虚拟主机
  • 基于域名的虚拟主机
  • 基于端口的虚拟主机

2 基于IP的虚拟主机

基于Unix发展而来的系统,一般都支持在一块网卡上绑定多个IP地址。也就是所谓的IP别名了。

2.1 绑定多个IP地址

此处本人给该主机添加两个额外的IP地址

ifconfig eth0:1 192.168.161.127 broadcast 192.168.161.255 netmask 255.255.255.0 up
route add -host 192.168.161.127 dev eth0:1
ifconfig eth0:2 192.168.161.126 broadcast 192.168.161.255 netmask 255.255.255.0 up
route add -host 192.168.161.126 dev eth0:2

此时的IP信息如下

[root@h1 nginx]# ifconfig 
# 原来就有的IP
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D5:A3:78  
          inet addr:192.168.161.128  Bcast:192.168.161.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fed5:a378/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1740 errors:0 dropped:0 overruns:0 frame:0
          TX packets:857 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:143515 (140.1 KiB)  TX bytes:122079 (119.2 KiB)

# 新添加的
eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:D5:A3:78  
          inet addr:192.168.161.127  Bcast:192.168.161.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

# 新添加的
eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:D5:A3:78  
          inet addr:192.168.161.126  Bcast:192.168.161.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

# 本地回环地址
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

2.2 配置基于IP的虚拟主机

编辑配置文件/etc/nginx/nginx.conf

server {
    listen      192.168.161.126:80;
    server_name 192.168.161.126;
    access_log  /logs/ip-server1.access.log combined;
    location / {
        index index.html;
        root /web/html/1;
    } 
}


server {
        listen          192.168.161.127:80;
        server_name     192.168.161.127;
        access_log      /logs/ip-server2.access.log combined;
        location / {
                index index.html;
                root /web/html/2;
        } 
}


server {
        listen          192.168.161.128:80;
        server_name     192.168.161.128;
        access_log      /logs/ip-server3.access.log combined;
        location / {
                index index.html;
                root /web/html/3;
        } 
}

建立目录结构

[root@h1 /]# tree web -L 3
web
└── html
    ├── 1
    │   └── index.html
    ├── 2
    │   └── index.html
    └── 3
        └── index.html

测试

分别用浏览器访问:
http://192.168.161.126
http://192.168.161.127
http://192.168.161.128

2.3 总结

此处的一个 server{...} 段就代表一个虚拟主机。这里只是做了简单的配置。
其他配置选项可以参考本人该系列的其他文章。

另外,众所周知,IPV4地址总共就40多亿个。
这种基于IP的虚拟主机,如果在内网使用,倒是还蛮不错的。
如果一旦放到公网,这IP地址就……

3 基于域名的虚拟主机

3.1 域名处理

此处由于本人没有公网主机IP,即使是有个域名也是白搭。
所有此处就暂时修改hosts文件来弄个虚假的域名试试了。

在hosts文件加入如下内容

192.168.161.128     www.hylexus.com hylexus.com aaa.hylexus.com bbb.hylexus.com

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

# 处理二级域名aaa.hylexus.com的请求
server{
    listen  80;
    server_name aaa.hylexus.com;
    access_log  /logs/aaa.access.log;
    location /{
        index index.html;
        root /web/html/aaa.hylexus.com;
    }
}

# 处理二级域名bbb.hylexus.com的请求
server{
    listen  80;
    server_name bbb.hylexus.com;
    access_log  /logs/bbb.access.log;
    location /{
        index index.html;
        root /web/html/bbb.hylexus.com;
    }
}

# 处理域名www.hylexus.com,hylexus.com,和除了{aaa,bbb}/hylexus.com的请求
server{
    listen  80;
    server_name www.hylexus.com hylexus.com *.hylexus.com;
    access_log  /logs/bbb.access.log;
    location /{
        index index.html;
        root /web/html/www.hylexus.com;
    }
}

3.3 总结

域名也不是很贵,看你要什么样的了。有一年两块钱的也有上万的。
只要有个域名和公网主机IP就好办了。一个IP就可以为多个域名服务了。
买个域名一般要比搞个公网IP划算多了啊。

这种基于域名的虚拟主机也是最常见的。

比如:

http://www.apache.org/
http://apache.org/
http://tomcat.apache.org/
http://spark.apache.org/
http://hadoop.apache.org/

4 基于端口的虚拟主机

4.1 基于端口的虚拟主机配置

server{
    listen  80;
    server_name 192.168.161.128;
    access_log  /logs/aaa.access.log;
    location /{
        index index.html;
        root /web/html/aaa.hylexus.com;
    }
}

server{
    listen  8080;
    server_name 192.168.161.128;
    access_log  /logs/bbb.access.log;
    location /{
        index index.html;
        root /web/html/bbb.hylexus.com;
    }
}

server{
    listen  8090;
    server_name 192.168.161.128;
    access_log  /logs/bbb.access.log;
    location /{
        index index.html;
        root /web/html/www.hylexus.com;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值