• Nginx虚拟主机的搭建 基于ip 基于端口 基于域名


    一、虚拟主机介绍

    虚拟主机是一种特殊的软硬件技术,他可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以单独对外提供web服务,这样就可以实现一台主机对多个web服务,每个虚拟主机都是独立的,互相不影响

    nginx支持三种类型的虚拟主机配置

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

    二、基于端口

    创建web目录

    [root@localhost ~]# mkdir -p /var/www/html
    
    • 1

    修改配置文件

    [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    server {
       listen       80;
       server_name  localhost
        location / {
           root   /usr/share/nginx/html;
           index  index.html index.htm;
       }
    }
    server {
       listen       81;
       server_name  localhost;
       location / {
           root   /var/www/html;
           index  index.html ;
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    重启nginx服务

    [root@localhost html]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@localhost html]# nginx -s reload
    
    • 1
    • 2
    • 3
    • 4

    发布你的项目

    客户端访问

    在这里插入图片描述
    在这里插入图片描述

    三、基于域名

    修改配置文件

    server {
        listen       80;
        server_name  www.zhangxiao.com;
        location / {
            root   /usr/share/nginx/html;
            index  index.html ;
      }
    }
    server {
        listen       80;
        server_name  www.zx.com;
        location / {
            root   /var/www/html;
            index  index.html ;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    域名解析

    在这里插入图片描述
    修改该目录下的hosts文件

    在这里插入图片描述

    重启nginx服务

    [root@localhost html]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@localhost html]# nginx -s reload
    
    • 1
    • 2
    • 3
    • 4

    客户端浏览器访问

    在这里插入图片描述

    在这里插入图片描述

    四、基于端口

    添加一个可用ip

    [root@localhost html]# ip a a 192.168.142.136/24 dev  ens33
    
    • 1

    修改配置文件

    server {
        listen       80;
        server_name  192.168.142.135;
        location / {
            root   /usr/share/nginx/html;
            index  index.html ;
       }
    }
    server {
        listen       80;
        server_name  192.168.20.136;
        location / {
            root   /var/www/html;
            index  index.html ;
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    重启nginx服务

    [root@localhost html]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
    • 1
    • 2
    • 3

    客户端浏览器访问

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    oracle触发器的自治事务
    sonar是一款静态代码质量分析工具
    CEC2013(MATLAB):猎豹优化算法(The Cheetah Optimizer,CO)求解CEC2013
    Spring6 的JdbcTemplate的JDBC模板类的详细使用说明
    【MCAL_CANDriver】-1.5-图解CANFD如何兼容经典Classical CAN 2.0及其解决方案
    JVM参数调优
    JavaSE---数组的定义与使用
    【SpringMVC】处理器的封装和请求寻找到对应处理器的过程
    CopyOnWriteArrayList源码分析
    机器学习_10、集成学习-AdaBoost
  • 原文地址:https://blog.csdn.net/2301_78315274/article/details/133963772