基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机
一、基于域名的 Nginx 虚拟主机
1、为虚拟主机提供域名解析
echo "192.168.80.10 www.kgc.com www.benet.com" >> /etc/hosts
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810213234725-306142328.png)
2、为虚拟主机准备网页文档
mkdir -p /var/www/html/benet
mkdir -p /var/www/html/kgc
echo"<h1>www.kgc.com</h1>" > /var/www/html/kgc/index.html
echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810213541881-754231263.png)
3、修改Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
....
http {
.....
server {
listen 80;
server_name WWW.kgc.com; #设置域名www. kgc. com
charset utf-8;
access_1og logs/www.kgc.access.log; #设置日志名
location / {
root /var/www/html/kgc; #设置www.kgc.com的工作目录
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server
listen 80;
server_name www.benet.com; #设置域名www.benet.com
charset utf-8;
access_log logs/www.benet.access.1og;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x. html;
location = 50x.html{
root html;
}
}
}
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810214937166-543113661.png)
4、重启服务,访问测试
systemctl restart nginx.service
浏跑器访问
http://www.kgc.com
http://www.benet.com
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810230314485-1667677918.png)
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810215859900-1145574660.png)
浏览器验证
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810222952087-400349811.png)
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810223029833-1692257624.png)
二、基于IP的 Nginx 虚拟主机
1、设置虚拟网卡
iflconfig ens33:0 192.168.80.11 netmask 255.255.255.0 # 设置虚拟网卡
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810223808200-271339902.png)
2、配置基于IP的虚拟主机
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.70.5:80; #设置监听地址
server_name www.lic.com;
charset utf-8;
access_log logs/www.lic.access.log;
location / {
root /var/www/html/kgc;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.70.5:80; #设置监听地址
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.access.log;
location / {
root /var/www/html/accp;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810224203233-194048420.png)
3、重启服务,访问测试
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810224346802-392617465.png)
浏览器验证
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810224605330-1476488796.png)
三、基于端口的 Nginx 虚拟主机
1、基于端口配置nginx主机文件
vim /usr/local/nginx/conf/nginx.conf
....
http {
.....
server {
listen 192.168.70.5:8080; #设置监听8080 端口.
server_name www.nj.com;
charset utf-8;
access_log logs/www.nj.acess.1og;
location / {
root /var/www/html/nj;
index index.html index.php;
}
error_page 500 502 503 504 /50x. html;
location = 50x. html{
root html;
}
}
server
listen 192.168.70.5:8888;
server_name www.benet.com; #设置域名www.benet.com
charset utf-8;
access_log logs/www.benet.access.1og;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x. html;
location = 50x.html{
root html;
}
}
}
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810225710451-823937962.png)
2.重启服务,访问测试
systemctl restart nginx.service # 重启服务
浏览器访问
http://192.168.70.5:8080
http://192.168.70.5:8888
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810225926669-264391747.png)
浏览器验证
![](https://img2020.cnblogs.com/blog/2391841/202108/2391841-20210810230113682-1997949251.png)