第七章 Nginx配置虚拟主机

一、配置方式

#虚拟主机配置方式:
1.基于多IP的方式
2.基于多端口的方式
3.基于多域名的方式

二、基于多IP的方式

1.第一个配置文件
[root@web02 /etc/nginx/conf.d]# vim mali.conf 
server {
    listen 10.0.0.8:80;
    server_name localhost;
    location / {
        root /code/zhiwu;
        index index.html;
    }
}

2.第二个配置文件
[root@web02 /etc/nginx/conf.d]# vim tank.conf 
server {
    listen 172.16.1.8:80;
    server_name localhost;
    location / {
        root /code/tank;
        index index.html;
    }
}

3.检查配置重启
[root@web02 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web02 /etc/nginx/conf.d]# systemctl restart nginx

三、基于多端口的方式

1.第一个配置
[root@web02 /etc/nginx/conf.d]# cat mali.conf 
server {
    listen 80;
    server_name localhost;
    location / {
        root /code/zhiwu;
        index index.html;
    }
}

2.第二个配置
[root@web02 /etc/nginx/conf.d]# cat tank.conf 
server {
    listen 81;
    server_name localhost;
    location / {
        root /code/tank;
        index index.html;
    }
}

3.检查配置并重启
[root@web02 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web02 /etc/nginx/conf.d]# systemctl restart nginx

4.访问测试
1)访问 http://10.0.0.8:80
2)访问 http://10.0.0.8:81

四、基于多域名的方式

1.第一个配置
[root@web02 /etc/nginx/conf.d]# vim mali.conf 
server {
    listen 80;
    server_name www.mali.com;
    location / {
        root /code/zhiwu;
        index index.html;
    }
}

2.第二个配置
[root@web02 /etc/nginx/conf.d]# vim tank.conf 
server {
    listen 80;
    server_name www.tank.com;
    location / {
        root /code/tank;
        index index.html;
    }
}
3.检查并重启
[root@web03 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web03 /etc/nginx/conf.d]# systemctl restart nginx

4.配置本地hosts
C:\Windows\System32\drivers\etc\hosts
10.0.0.8 www.mali.com
10.0.0.8 www.tank.com

5.访问测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值