Nginx虚拟主机配置

目录

1.什么是虚拟主机?

2.准备工作(本机IP:10.12.153.222)

3.基于端口配置

4.基于IP配置

5.基于域名配置


1.什么是虚拟主机?

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。

nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。

1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)

2、基于ip的虚拟主机, (一台主机绑定多个ip地址)

3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

2.准备工作(本机IP:10.12.153.222)

1.查看nginx是否启动

2.在/usr/share/nginx/html/下创建aaa,bbb,ccc三个目录并在aaa,bbb,ccc下分别创建index.html文件并写入不同的内容用来测试虚拟主机是否部署成功

3.基于端口配置

vim /etc/nginx/conf.d/nginx.conf(若没有自己创建)

server {
    listen      80;  #端口
    server_name     localhost;  #域名
    location / 
    {
    root       /usr/share/nginx/html/aaa;
    index index.html index.htm;
    }
}

server {
    listen      90;
    server_name     localhost;
    location /
    {
    root       /usr/share/nginx/html/bbb;
    index index.html index.htm;
    }
}

server {
    listen      100;
    server_name     localhost;
    location /
    {
    root        /usr/share/nginx/html/ccc;
    index index.html index.htm;
    }
}

浏览器访问IP:80  IP:90  IP:100 

若分别出现aaa,bbb,ccc下index.html的内容为部署成功

4.基于IP配置

1.为服务器添加两个IP

ip a a 10.12.153.220/24 dev ens33

ip a a 10.12.153.221/24 dev ens33

server {
    listen 10.12.153.220:80;  #端口
    server_name     localhost;  #域名
    location / 
    {
    root       /usr/share/nginx/html/aaa;
    index index.html index.htm;
    }
}

server {
    listen  10.12.153.221:80;
    server_name     localhost;
    location /
    {
    root       /usr/share/nginx/html/bbb;
    index index.html index.htm;
    }
}

server {
    listen  10.12.153.222:80;
    server_name     localhost;
    location /
    {
    root        /usr/share/nginx/html/ccc;
    index index.html index.htm;
    }
}

浏览器访问10.12.153.220,10.12.153.221,10.12.153.222

若分别出现aaa,bbb,ccc下index.html的内容为部署成功

5.基于域名配置

server {
    listen      80;  #端口
    server_name     www.test.com;  #域名
    location / 
    {
    root       /usr/share/nginx/html/aaa;
    index index.html index.htm;
    }
}

server {
    listen      80;
    server_name     www.test1.com;
    location /
    {
    root       /usr/share/nginx/html/bbb;
    index index.html index.htm;
    }
}

server {
    listen      80;
    server_name    www.test2.com;
    location /
    {
    root        /usr/share/nginx/html/ccc;
    index index.html index.htm;
    }
}

#注意 使用域名时需本地解析

Windows解析路径

C:\Windows\System32\drivers\etc\hosts

10.12.153.222  www.test.com  

10.12.153.222  www.test1.com 

10.12.153.222  www.test2.com

浏览器分别访问www.test.com,www.test1.com,www.test2.com

若分别出现aaa,bbb,ccc下index.html的内容为部署成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值