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 分钟前
虚拟化技术
网络·智能路由器
FrozenLove_G1 小时前
机械硬盘的工作原理
服务器·常识
张北涛2 小时前
交换机详细
运维·服务器·网络
IT19952 小时前
Wireshark笔记-DHCP两步交互流程与数据解析
网络·笔记·wireshark
小马哥编程3 小时前
计算机网络:网络设备在OSI七层模型中的工作层次和传输协议
网络·计算机网络·智能路由器
误入运维泥潭3 小时前
LVS、Nginx与HAProxy负载均衡技术对比介绍
nginx·lvs·haproxy·keealived
学会煎墙3 小时前
3分钟快速入门WebSocket
网络·websocket·网络协议
专注代码七年3 小时前
Docker 本地开发环境搭建(MySQL5.7 + Redis7 + Nginx + 达梦8)- Windows11 版 2.0
nginx·docker·容器
白堤上的喵3 小时前
信息安全基础知识
网络
liulilittle4 小时前
UNIX/macOS路由表查询原理与实现
服务器·开发语言·c++·macos·unix·编程语言