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地址更改了后面的端口