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

客户端浏览器访问


相关推荐
hhhjhl6 小时前
Protobuf.js 跨端三方库仓颉适配全流程
javascript·网络·udp
琥珀.6 小时前
mysql中json数组转列表查询
linux·运维·服务器
ITxiaobing20237 小时前
中国公司海外广告投放的 IP 地址库选型:从“查归属”到“验真实”的工程视角
网络·网络协议·tcp/ip
KKKlucifer8 小时前
运营商非结构化文档安全管控体系建设实践
大数据·网络·安全
云边云科技_云网融合9 小时前
连锁门店组网怎么做?SD-WAN 多分支组网的架构选型、部署与成本结构
服务器·数据库·php
网络小江9 小时前
组网第35课:静态路由——下一跳、出接口、默认路由、浮动静态与黑洞路由
服务器·网络·默认路由·黑洞路由·浮动静态路由
acrel710 小时前
医院用电隐患治理与节能降耗及智慧能源平台应用
网络·数据库
叼烟扛炮10 小时前
Linux 学习笔记(三十三):线程互斥——互斥锁(Mutex)与死锁
linux·运维·服务器·笔记·学习·1024程序员节
无足鸟ICT10 小时前
【RHCA+】who命令(查看已登陆用户)
linux·运维·服务器
Escalating_xu10 小时前
【Linux 网络编程】应用层协议 HTTP(下):Header、连接管理与 C++ HTTP 服务器
linux·服务器·网络