nginx虚拟主机

nginx虚拟主机

虚拟主机有三种:基于域名的虚拟主机,基于IP地址的虚拟主机,基于端口的虚拟主机

做虚拟主机的目的是:在同一台服务器上,访问不同的域名、IP地址或端口,能够访问到不同的页面。

实验环境:
nginx-1.16.1
192.168.91.131 nginx

实验步骤如下:

基础环境:

1、关闭selinux和防火墙

2、安装nginx环境(可源码编译,可yum安装,在本实验我们选择yum安装)

[root@localhost ~]# yum install yum-utils
[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo   #配置nginx的yum源
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[root@localhost ~]# yum -y install nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx

3、测试基础环境

1

实验一:基于域名的虚拟主机

1、修改配置文件

[root@localhost ~]# cd/etc/nginx/conf.d/
[root@localhost ~]# mv default.conf default.conf.bak #备份原文件
[root@localhost ~]# vim default.conf
server {
    listen       80;
    server_name  www.nginx1.com;
    location / {
        root   /web1/;
        index  index.html index.htm;
    }
}    
server {
    listen       80;
    server_name  www.nginx2.com;
    location / {
        root   /web2/;
        index  index.html index.htm;
    }
}

2、创建目录写入测试页面

[root@localhost conf.d]# mkdir /web{1..2}
[root@localhost conf.d]# vim /web1/index.html
<html>
  <h1>i am nginx1</h1>
  <h2>www.nginx1.com</h2>
</html>
            
[root@localhost conf.d]# vim /web1/index.html   
<html>
  <h1>i am nginx2</h1>
  <h2>www.nginx2.com</h2>
</html>

[root@localhost conf.d]# nginx -s reload            

3、测试

(1)方法一:直接使用curl查看

[root@localhost ~]# curl www.nginx1.com
<html>
  <h1>i am nginx1</h1>
  <h2>www.nginx1.com</h2>
</html>
~             
[root@localhost ~]# curl www.nginx2.com
<html>
  <h1>i am nginx2</h1>
  <h2>www.nginx2.com</h2>
</html>

(2)在浏览器查看

需要在C:\Windows\System32\drivers\etc\hosts 文件中添加两行

192.168.91.131    www.nginx1.com
192.168.91.131    www.nginx2.com

访问不同域名得到不同的页面。
2
3

实验二:基于IP地址的虚拟主机

在实验一的基础上做,修改配置文件

[root@localhost ~]# ifconfig ens33:1 192.168.91.231/24
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.131  netmask 255.255.255.0  broadcast 192.168.91.255

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.91.231  netmask 255.255.255.0  broadcast 192.168.91.255
[root@localhost ~]# vim /web1/index.html 
server {
    listen       80;
    server_name  192.168.91.131;

    location / {
        root   /web1/;
        index  index.html index.htm;
    }
}
server {
    listen       80;
    server_name  192.168.91.231;

    location / {
        root   /web2/;
        index  index.html index.htm;
    }
} 
[root@localhost ~]# nginx -s reload

测试

45

补充:
删除绑定的子网ip
[root@localhost ~]# ifconfig ens33:1 192.168.91.131/24 down
重启一下nginx
[root@localhost ~]# systemctl restart nginx
实验三:基于端口的虚拟主机

在实验三的基础上做,修改配置文件

[root@localhost ~]# vim /web1/index.html 
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /web1/;
        index  index.html index.htm;
    }
}
server {
    listen       81;
    server_name  localhost;

    location / {
        root   /web2/;
        index  index.html index.htm;
    }
} 
[root@localhost ~]# nginx -s reload

测试

46
至此三种类型的虚拟主机配置完成。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值