一、实验环境及工具介绍
- 实验环境:VMware Workstation 15.5、Centos7.6、XShell 6
- 本次所需要的手动编译安装的nginx软件包下载地址:https://wwa.lanzous.com/i1720fk557c
二、实验一:基于域名
1、编译安装nginx,安装DNS服务
2、将域名www.test.com与www.test1.com分别进行解析
3、编辑nginx配置文件,添加虚拟主机
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
在配置文件末尾添加以下内容:
server {
server_name www.test.com;
location / {
root /var/www/test;
index index.html index.php;
}
}
server {
server_name www.test1.com;
location / {
root /var/www/test1;
index index.html index.php;
}
}
4、因为在虚拟主机中指明了站点的目录,所以需要手动创建站点页面
[root@localhost html]# cd /var/
[root@localhost var]# mkdir www ## 创建站点根目录
[root@localhost www]# mkdir test test1 ## 创建test、test两个站点目录
[root@localhost www]# cd test ## 进入test目录
[root@localhost test]# vim index.html ## 创建并编辑站点文件
写入以下内容:
<h1>Test Web</h1>
[root@localhost test]# cd ../test1 ## 进入test1站点目录
[root@localhost test1]# vim index.html ## 创建并编辑站点文件
写入如下内容:
<h1>Test1 Web</h1>
5、重启nginx服务
[root@localhost test1]# systemctl restart nginx
6、打开浏览器,分别访问www.test.com与www.test1.com
三、实验二:基于端口
1、编辑nginx配置文件,基于实验一修改虚拟机主机的配置
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf ## 编辑配置文件
server {
server_name 192.168.50.134:5555; ## 将域名修改为IP地址+端口
listen 192.168.50.134:5555; ## 增加这一行,内容为IP地址+监听端口
location / {
root /var/www/test;
index index.html index.php;
}
}
server {
server_name 192.168.50.134:4444; ## 同上
listen 192.168.50.134:4444; ## 同上
location / {
root /var/www/test1;
index index.html index.php;
}
}
2、重启nginx服务
[root@localhost /]# systemctl restart nginx
3、打开浏览器,清空缓存,分别访问192.168.50.134:4444与192.168.50.134:5555
四、实验三:基于IP
1、在VM虚拟机中添加一块网卡并配置一个IP地址(192.168.50.135)
2、编辑nginx配置文件,基于实验二修改虚拟机主机的配置
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf ## 编辑配置文件
server {
server_name 192.168.50.134:80; ## IP地址+80端口
listen 192.168.50.134:80; ## IP地址+80端口
location / {
root /var/www/test;
index index.html index.php;
}
}
server {
server_name 192.168.50.135:80; ## 另一个IP地址+80端口
listen 192.168.50.135:80; ## 另一个IP地址+监听端口80
location / {
root /var/www/test1;
index index.html index.php;
}
}
3、重启nginx服务
[root@localhost /]# systemctl restart nginx