基于nginx的虚拟主机配置

目录

一.基于不同ip的虚拟主机

二.基于不同端口的虚拟主机

三.基于不同域名的虚拟主机


一.基于不同ip的虚拟主机

1.关闭 SELinux和防火墙

2.在/data目录中创建三个目录,分别为nginx1、nginx2 和nginx3,具体名为:

3.分别在三个目录中创建index.html,并输入内容“hello, nginx1" 、“ hello, nginx2" 和
“hello,nginx3” ,具体命令如下: 

4.新建配置文件vhost.conf,并输入以下内容:

1)切换到主配置目录:

2)新建vhost.conf文件:

3)添加server信息:

server {
        listen 192.168.233.139:80; #监听的ip及端口
        server_name localhost;     #域名
        location / {               
           root /data/nginx1;      #文件目录位置
           index index.html;       #网页文件名
        }
}

server {
        listen 192.168.233.140:80;
        server_name localhost;
        location / {
         root /data/nginx2;
         index index.html;
        }
}

server {
        listen 192.168.233.141:80;
        server_name localhost
        location / {
          root /data/nginx3;
          index index.html;
        }


5.在虚拟机中添加ip地址:

1)添加ip:

2)启动网卡:

3)检查配置文件语法:

4)重新加载配置文件:

6.访问网页

二.基于不同端口的虚拟主机

1,2,3同ip的配置

4的1,2同ip的配置

4.3vhost.conf文件:

server {
        listen 80; #监听的端口
        server_name localhost;     #域名
        location / {               
           root /data/nginx1;      #文件目录位置
           index index.html;       #网页文件名
        }
}

server {
        listen 81;
        server_name localhost;
        location / {
         root /data/nginx2;
         index index.html;
        }
}

server {
        listen 82;
        server_name localhost
        location / {
          root /data/nginx3;
          index index.html;
        }

5的3,4同ip的配置

6.访问网页

三.基于不同域名的虚拟主机

1,2,3同ip的配置

4的1,2同ip的配置

4.3vhost.conf文件:

server {
        listen 80;
        server_name www1.nginx.com;
        location / {
           root /data/nginx1;
           index index.html;
        }
}

server {
        listen 80;
        server_name www2.nginx.com;
        location / {
         root /data/nginx2;
         index index.html;
        }
}

server {
        listen 80;
        server_name www3.nginx.com;
        location / {
          root /data/nginx3;
          index index.html;
        }
}
                                           

5.域名配置

添加域名:

6.同5的3,4

7.访问网页

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值