Nginx三种虚拟主机

Nginx 支持的虚拟主机

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

##通过“server{}”配置端实现

1.首先我们先用hosts工具用一个ip添加俩个不同的域名
在这里插入图片描述

也可以通过修改windos客户机的C:\Windows\System32\drivers\etc\hosts文件,加入www.51xit.top和www.52xit.top这两个域名,它们都指向同-个服务器IP地址,用于实现不同的域名访问不同的虚拟主机。

20.0.0.26 www.51xit.top www.52xit.top

在这里插入图片描述
修改好之后用真机ping域名测试一下,发现都是可以ping通的

准备各个网站的目录和测试首页

[root@localhost ~]# mkdir -p /var/www/html/51xit 创建www.bt.com的根目录
[root@localhost ~]# mkdir -p /var/www/html/52xit 创建www.test.com的根目录
[root@localhost ~]# echo “www.51xit.top” >> /var/www/html/5
51xit/ 52xit/
[root@localhost ~]# echo “www.51xit.top” >> /var/www/html/51xit/index.html
[root@localhost ~]# echo “www.52xit.top” >> /var/www/html/52xit/index.html
[root@localhost ~]# cat /var/www/html/52xit/index.html
www.52xit.top
[root@localhost ~]# cat /var/www/html/51xit/index.html
www.51xit.top
[root@localhost ~]# cat /var/www/html/51xit/index.html
www.51xit.top

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

修改配置文件,把配置文件中的server{}代码段全部去掉,加入2个新的server{}段,对应2个域名

server {
        listen 80;
        server_name www.51xit.top;
        charset utf-8;
        access_log logs/www.51xit.top.access.log;
        location /{
                root /var/www/html/51xit;
                index index.html index.htm;
                }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
              root html;
         }
}

server {
        listen 80;
        server_name www.52xit.top;
        charset utf-8;
        access_log logs/www.52xit.top.access.log;
        location /{
                root /var/www/html/52xit;
                index index.html index.htm;
                }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
              root html;
         }
}

[root@localhost ~]# systemctl restart nginx 重新一下服务

用真机的浏览器输入域名测试一下
在这里插入图片描述
在这里插入图片描述
接下来基于ip的虚拟Web主机
因为有俩个域名所以做该实验的时候,我们要在虚拟主机中再加一块网卡
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这样还没有结束,我们还要在虚拟机上修改一下第二张网卡的一些配置
在这里插入图片描述
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ll
在这里插入图片描述

里面有网卡ens33文件
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36 把ens33文件的内容拷贝到ens36中去
[root@localhost network-scripts]# vi ifcfg-ens36
在这里插入图片描述
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifup ens36
[root@localhost network-scripts]# ifconfig

在这里插入图片描述
现在就有ens36的网卡信息了,那我们网卡就添加好了
[root@localhost network-scripts]# systemctl restart nginx 修改完成之后重启一下
在这里插入图片描述

在这里插入图片描述

用真机直接用ip地址就可以访问了

在这里插入图片描述
基于端口虚拟Web主机
[root@localhost network-scripts]# vi /usr/local/nginx/conf/nginx.conf

在这里插入图片描述
在这里插入图片描述
一个ip地址更改了后面的端口
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值