美文网首页PHP经验分享
nginx系列-03-虚拟主机的配置

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

作者: hylexus | 来源:发表于2016-10-24 23:33 被阅读51次

[TOC]

** 以下所有的测试都是在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;
    }
}

相关文章

  • nginx

    nginx的配置、虚拟主机、负载均衡和反向代理一nginx的配置、虚拟主机、负载均衡和反向代理二nginx的配置、...

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

    [TOC] ** 以下所有的测试都是在CentOS6系统上进行的 ** ** 另外,本篇文章只配置最基本的配置项。...

  • Nginx 虚拟主机配置和日志管理 [2]

    一、nginx虚拟主机配置 Nginx虚拟主机对应的文件实体就是/usr/local/nginx/nginx.co...

  • Nginx——虚拟主机配置

    基于域名的虚拟主机 基于端口的虚拟主机 基于IP的虚拟主机nginx的主配置文件/etc/nginx/nginx....

  • Nginx 常用配置

    1. Nginx 配置虚拟主机 1.1 基于 IP 的虚拟主机 修改配置文件 nginx.conf 重启服务 测试...

  • Nginx

    安装Nginx 配置虚拟主机

  • nginx2-路由

    1.nginx管理虚拟主机 基于域名虚拟主机配置 基于端口虚拟主机配置 基于ip虚拟主机配置 2.Location...

  • Nginx虚拟主机与安全认证实战

    知识要点: 配置Nginx虚拟主机实践 安全认证实践 安装验证 Nginx虚拟主机配置讲解 讲解配置之前我们要了解...

  • Nginx 虚拟主机

    什么是虚拟主机 Nginx 配置文件的结构 基于 IP 的虚拟主机配置 基于端口的虚拟主机配置 基于域名的虚拟主机...

  • Nginx location

    上一篇 Nginx 虚拟主机 Nginx location  在 nginx 的配置文件中,经常可以看到虚拟主机配...

网友评论

    本文标题:nginx系列-03-虚拟主机的配置

    本文链接:https://www.haomeiwen.com/subject/ygxruttx.html