web服务器-nginx虚拟主机

web服务器-nginx虚拟主机

一 虚拟主机介绍

  • 就是把一台物理服务器划分成多个虚拟的服务器, 每一个虚拟主机都可以有独立的域名和独立的目录,同时发布俩个网站.

二. 基于IP的虚拟主机

  • 应用场景:适用于IP充足的环境

    server {
        listen       192.168.157.15:80;
        location / {
            root   html/web1;
            index  index.html index.htm index.php;
        }
    }
    server {
        listen       192.168.157.20:80;
    location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
    
  • 使用ifconfig创建虚拟ip别名

    ifconfig ens33:1 192.168.157.20
    
    
    
    ens33:x                    //虚拟网络接口,建立在ens33上,取值范围0-255
    192.168.6.xxx      //增加ip别名,想加多少就加多少
    

    实际情况是有多个ip来进行操作, 这里测试直接创建

    清除ip别名

    #ifconfg ens33:0 down
    #ifconfg ens33:1 down
    #ifconfg ens33:2 down
    

    别名设置详情网址:https://www.cnblogs.com/wanghuaijun/p/6155832.html

  • 测试

    elinks http://192.168.157.15/index.html
    
    
    
    • 注意: 这里我犯了个错误, 配置文件中

      root  html/web2   #表示输入ip直接指向web2下的文件, 这里访问的时候就不需要再输入http://192.168.157.15/web1/index.html
      
  • 基于IP的虚拟主机特点

    • 不同IP对应不同的网站
    • 访问方便,用户直接使用默认端口即可访问
    • 服务器需要有多个IP地址
    • 维护方便,基于独立的IP的站点,便于监控,维护

三. 基于端口的虚拟主机

  • 只需要一个IP

  • 缺点:端口你是无法告诉公网客户, 无法适用于公网客户,适用内部用户使用

    基于端口
    server {
        listen       80;
        #server_name  www.abc.com;
        location / {
            root   html/web1;
            index  index.html index.htm index.php;
        }
    }
    server {
        listen       8080;
        #server_name  www.abc.com;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
    
  • 基于端口的虚拟主机的特点

    • 不同的端口对应不同的网站
    • 访问需要加端口
    • 节省IP地址
    • 适用于私网允许

四 基于域名的虚拟主机

  • 一个网站必然有多个域名

    基于域名
    server {
        listen       80;
        server_name  web1.ayitula.com;
        location / {
            root   html/web1;
            index  index.html index.htm index.php;
        }
    }
    server {
        listen       80;
        server_name  web2.ayitula.com;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
    
  • 本地测试需要在/etc/hosts文件中做域名解析

    vim /etc/hosts
    
    
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.157.15 web1.hexin.com
    192.168.157.15 web2.hexin.com
    
    
  • 基于域名的虚拟主机的特点

    • 不同域名对应不同的网站
    • 需要多个域名,可以是二级或者三级域名
    • 每个站点适用默认端口,方便访问
    • 只需要一个IP地址,节约成本
    • 适用公网环境
posted @ 2020-03-06 16:09  云丛  阅读(396)  评论(0编辑  收藏  举报