安装Nginx

yum install nginx -y

进入已配置好的网站根目录,创建两个测试文件夹

文件夹中用于存放测试网站信息,即存放项目代码。
cd /usr/share/nginx/html
mkdir Testpage-1
mkdir Testpage-2

创建并编辑index.html文件

index.html为将要显示的页面,同样可以把网站项目直接放在/usr/share/nginx/html/目录下,直接使用
cd /usr/share/nginx/html/Testpage-1/
vim index.html
按i进入编辑模式,输入以下测试内容。
Test page 1
:wq!保存
cd /usr/share/nginx/html/Testpage-2/
vim index.html
按i进入编辑模式,输入以下测试内容。
Test page 2
:wq!保存

配置Nginx

运行以下命令查看nginx.conf配置文件。
cat /etc/nginx/nginx.conf
其中include /etc/nginx/conf.d/*.conf;表示Nginx将会从该路径下的所有.conf文件获取站点信息。
为测试站点Testpage-1创建并配置Nginx配置文件。
运行以下命令进入/etc/nginx/conf.d路径下
cd /etc/nginx/conf.d
为试站点Testpage-1创建并配置Nginx配置文件。
vim Testpage1.conf
按i进入编辑模式,输入以下内容。
在注释内容处,替换服务器域名参数信息与项目路径参数信息。
server {
listen 80;
server_name testpage1.com; #此处使用测试域名。实际配置中使用您的服务器域名。

#charset koi8-r;
access_log /var/log/nginx/b.access.log main;

location / {
root /usr/share/nginx/html/Testpage-1; #测试站点路径。即您的项目代码路径。
index index.html index.htm;
}

#error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
为测试站点Testpage-2创建并配置Nginx配置文件。
运行以下命令进入/etc/nginx/conf.d路径下
cd /etc/nginx/conf.d
为试站点Testpage-1创建并配置Nginx配置文件。
vim Testpage2.conf
b.	按i进入编辑模式,输入以下内容。
在注释内容处,替换服务器域名参数信息与项目路径参数信息。
server {
listen 80;
server_name testpage2.com; #此处使用测试域名。实际配置中使用您的服务器域名。

#charset koi8-r;
access_log /var/log/nginx/b.access.log main;

location / {
root /usr/share/nginx/html/Testpage-2; #测试站点路径。即您的项目代码路径。
index index.html index.htm;
}

#error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

重启Nginx服务

systemctl restart nginx

本地配置hosts(配置了,可以以域名方式访问)

1. 访问C:\Windows\System32\drivers\etc目录。 2. 复制hosts文件进行备份。 保留hosts - 副本文件,在测试完成后使用该文件恢复hosts文件的初始状态。 3. 修改hosts文件。 在文件末尾追加以下内容。 IP地址 testpage1.com IP地址 testpage2.com 保存文件并退出。 4. 返回Windows桌面,并按下Win + R组合键。 5. 在运行对话框中输入cmd,并单击确定。 6. 在命令行中运行以下命令,使hosts配置立即生效。 ipconfig /flushdns 在本地主机打开浏览器,成功访问到两个测试站点。