基于(域名、IP、端口)三种形式 访问Nginx 虚拟主机

4 篇文章 0 订阅

一.基于域名的 Nginx 虚拟主机

1.为虚拟主机提供域名解析

echo "192.168.80.10 www.kgc.com www.benet.com" >> /etc/hosts

在这里插入图片描述

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

在这里插入图片描述

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;
   }
 }
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

4.重启服务,访问测试

systemctl restart nginx

浏跑器访问
http://www.kgc.com
http://www.benet.com

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

二.基于IP的 Nginx 虚拟主机

iflconfig ens33:0 192.168.80.11 netmask 255.255.255.0

在这里插入图片描述

vim /usr/local/nginx/conf/nginx.conf
....
http {
.....
   server {
     listen 192.168.116.90:80;    #设置监听地址192.168.80.10
     server_name WWW.nj.com; .
     charset utf-8;
     access_log logs/www.nj.access.log;
     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.116.91: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;
   }
 }
}

在这里插入图片描述

在这里插入图片描述

4.重启服务,访问测试

systemctl restart nginx

浏览器访问
http://192.168.116.90
http://192.168.116.91

在这里插入图片描述

三.基于端口的 Nginx 虚拟主机

vim /usr/local/nginx/conf/nginx.conf
....
http {
.....
    server {
    listen 192.168.116.90: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.116.91: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;
   }
 }
}

在这里插入图片描述
在这里插入图片描述
重启服务,访问测试

systemctl restart nginx

浏览器访问
http://192.168.116.90:8080
http://192.168.116.91:8888

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

W.scaler

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值