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

客户端浏览器访问


相关推荐
白码低代码12 分钟前
质检 LIMS 系统数据防护指南 三级等保认证与金融级加密方案设计
网络·金融·lims·实验室管理系统
jz_ddk1 小时前
[zynq] Zynq Linux 环境下 AXI BRAM 控制器驱动方法详解(代码示例)
linux·运维·c语言·网络·嵌入式硬件
深思慎考2 小时前
Linux网络——socket网络通信udp
linux·网络·udp
孤寂大仙v2 小时前
【计算机网络】NAT、代理服务器、内网穿透、内网打洞、局域网中交换机
网络·计算机网络·智能路由器
LuckyRich12 小时前
【websocket】安装与使用
网络·websocket·网络协议
KIDAKN2 小时前
理解网络协议
网络·网络协议
落笔画忧愁e2 小时前
扣子Coze飞书多维表插件-列出全部数据表
java·服务器·飞书
字节高级特工2 小时前
【Linux篇】0基础之学习操作系统进程
linux·运维·服务器·数据结构·windows·学习·list
s_little_monster3 小时前
【Linux】网络--数据链路层--以太网
linux·运维·网络·经验分享·笔记·学习·计算机网络
Adorable老犀牛4 小时前
负载均衡将https请求转发后端http服务报错:The plain HTTP request was sent to HTTPS port
nginx·http·https·负载均衡