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

一、虚拟主机介绍

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

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

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

二、基于端口

创建web目录

复制代码
[root@localhost ~]# mkdir -p /var/www/html

修改配置文件

复制代码
[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 ;
   }
}

重启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

发布你的项目

客户端访问


三、基于域名

修改配置文件

复制代码
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 ;
  }
}

域名解析


修改该目录下的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

客户端浏览器访问

四、基于端口

添加一个可用ip

复制代码
[root@localhost html]# ip a a 192.168.142.136/24 dev  ens33

修改配置文件

复制代码
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 ;
   }
}

重启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

客户端浏览器访问


相关推荐
每日出拳老爷子5 小时前
【远程协助】内网 IT 运维远程协助系统的最小可用架构
运维·服务器·远程工作·流媒体·视音频
CheungChunChiu5 小时前
Linux 内核动态打印机制详解
android·linux·服务器·前端·ubuntu
西***63476 小时前
打破部署桎梏!编码器两大核心架构(NVR/PoE)深度解析
服务器·音视频·视频编解码
小目标一个亿7 小时前
Windows平台Nginx配置web账号密码验证
linux·前端·nginx
Love Song残响7 小时前
深入解析TCP/IP协议栈:从底层到应用层
网络·tcp/ip·php
线束线缆组件品替网8 小时前
IO Audio Technologies 音频线缆抗干扰与带宽设计要点
网络·人工智能·汽车·电脑·音视频·材料工程
布谷歌9 小时前
开发笔记:如何消除秘钥数据对RPC负荷、日志、系统安全的伤害?
网络·笔记·网络协议·rpc
heartbeat..9 小时前
零基础学 SQL:DQL/DML/DDL/DCL 核心知识点汇总(附带连接云服务器数据库教程)
java·服务器·数据库·sql
咒法师无翅鱼9 小时前
【西电计网学习笔记】网络层【RIP,OSPF,ARP,ICMP,IGMP,逻辑寻址(ABCD四类)】
网络
Ares-Wang9 小时前
网络》》以太网交换安全
网络·安全