04---虚拟主机

 

虚拟主机

1、虚拟主机介绍

  • 现象

    一个web服务器软件默认情况下只能发布一个web,因为web分享出去需要三个条件IP、Port、Domain

  • 介绍

    虚拟主机:就是把一台服务器划分成多个虚拟的服务器,每一个虚拟主机都可以有独立的域名和独立的目录

  • 如何能发布多个web呢?

2、基于IP虚拟主机

  1. 两个IP:
    • 开启:ifconfig ens33:1 192.168.2.52/24 up
    • 关闭:ifconfig ens33:1 down
  2. DIR存在
  3. index.html存在

同时发布两个网站:

  • DocumentRoot:/usr/local/nginx/html/web1
  • DocumentRoot: /usr/local/nginx/html/web2

     server {
           listen       192.168.2.42:80;
           server_name  localhost;
           charset utf-8;
           location / {
               root   html/web01;
               index  index.html index.htm;
           }
           error_page   500 502 503 504  /50x.html;
           location = /50x.html {
               root   html;
           }
     }
     server {
           listen       192.168.2.52:80;
           server_name  localhost;
           charset utf-8;
           location / {
               root   html/web02;
               index  index.html index.htm;
           }
           error_page   500 502 503 504  /50x.html;
           location = /50x.html {
               root   html;
           }
     }

缺点:需要多个ip,如果是公网IP,每个IP都需要付费

3、基于端口的虚拟主机

  server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html/web01;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
  }
  server {
        listen       81;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html/web02;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
  }

缺点:只需要一个IP,但是端口你是无法告诉公网用户,无法适用于公网客户,适合内部用户

4、基于域名的虚拟主机

  • 修改:/etc/hosts/

      127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
      ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
      192.168.2.42 www.abc.com
      192.168.2.42 www.def.com
  • 修改:/nginx.conf/

    server {
          listen       80;
          server_name  www.abc.com;
          charset utf-8;
          location / {
              root   html/web01;
              index  index.html index.htm;
          }
          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
              root   html;
          }
    }
    server {
          listen       80;
          server_name  www.def.com;
          charset utf-8;
          location / {
              root   html/web02;
              index  index.html index.htm;
          }
          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
              root   html;
          }
     }
  • 测试:

     [root@Web01 nginx]# elinks http://www.abc.com -dump
     i am web01
     [root@Web01 nginx]# elinks http://www.def.com -dump
     i am web02
原文地址:https://www.cnblogs.com/xjmlove/p/10210007.html