zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    我不想安于当前的限度,以达到所谓的幸福,回顾下2020年的我
    CentOS 7 搭建 TinyProxy 代理 &&python 脚本访问
    使用国内源来安装pytorch速度很快
    opencv-python的格式转换 RGB与BGR互转
    自签SSL证书以及https的双向认证 实现nginx双向代理
    springboot使用 @EnableScheduling、@Scheduled开启定时任务
    微信下载对账单
    SpringBoot 中定时执行注解(@Scheduled、@EnableScheduling)
    使用idea合并 dev分支合并到test分支
    .Net Core + Entity Framework 调用Oracle 存储过程
  • 原文地址:https://www.cnblogs.com/xjmlove/p/10210007.html
Copyright © 2011-2022 走看看