nginx虚拟主机配置

什么是虚拟主机

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

Nginx虚拟主机配置

Nginx配置解析的三种方式:

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

1> 基于ip的虚拟主机(一块主机绑定多个ip地址)
2> 基于域名的虚拟主机(servername)
3> 基于端口的虚拟主机(listen如果不写ip端口模式)

例如:基于虚拟机ip的配置,这里需要配置多个ip

server
{
    listen 192.168.20.20:80;
    server_name www.linuxidc.com;
    root /data/www;
}
server
{
    listen 192.168.20.21:80;
    server_name www.linuxidc.com;
    root /data/www;
}

1、基于域名的虚拟主机

1.1、进入tengine目录

[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# ll

1.2、更改nginx.conf文件

[root@node1 conf]# vi nginx.conf

    server {
        listen       80;
        server_name  www.matrix.com;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }  
     server {
        listen       80;
        server_name  www.matrix2.com;

        location / {
            root   /opt/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

1.3、配置第二个虚拟主机的访问页面
[root@node1 conf]# cd /opt

[root@node1 opt]# mkdir html

[root@node1 opt]# cd html

将<html><h1>Hello World!</h1></html>内容复制到index.html文件中

[root@node1 html]# vim index.html
1.4、重新nginx服务
[root@node1 html]# service nginx restart

1.5、在window上进行ip地址映射【配置路由表】
【编辑C:\Windows\System32\drivers\etc\hosts文件】
 192.168.230.10 www.matrix.com
 192.168.230.10 www.matrix2.com

1.6、查看配置是否成功
在地址栏输入:www.matrix.com

在地址栏输入:www.matrix2.com

2、基于ip的虚拟主机

2.1、进入tengine目录
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# ll
2.2、更改nginx.conf文件
一个主机对应多个ip地址
[root@node1 conf]# vi nginx.conf

    server {
        listen       192.168.230.10:80;
        server_name  www.sparsematrix.com;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }  
     server {
        listen       192.168.230.18:80;
        server_name  www.sparsematrix2.com;

        location / {
            root   /opt/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

2.3、在Linux主机中设置虚拟ip

[root@node1 html]# ifconfig eth0:1 192.168.230.18 netmask 255.255.255.0

2.4、重启nginx服务

[root@node1 html]# service nginx restart

2.5、在window上进行ip地址映射
【编辑C:\Windows\System32\drivers\etc\hosts文件】
 192.168.230.10 www.sparsematrix.com
 192.168.230.18 www.sparsematrix.com

2.6、查看配置是否成功
在地址栏输入:http://http://192.168.230.10/

在地址栏输入:http://http://192.168.230.18/

3、基于端口的虚拟主机

3.1、进入tengine目录
[root@node1 ~]# cd /opt/modules/tengine-2.1.0/conf
[root@node1 conf]# ll
3.2、更改nginx.conf文件
[root@node1 conf]# vi nginx.conf

  server {
        listen       192.168.230.10:80;
        server_name  www.sparsematrix.com;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }  
     server {
        listen       192.168.230.10:8088;
        server_name  www.sparsematrix.com;

        location / {
            root   /opt/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

3.4、重启nginx服务
[root@node1 html]# service nginx restart

3.5、在window上进行ip地址映射
【编辑C:\Windows\System32\drivers\etc\hosts文件】
 192.168.230.10 www.sparsematrix.com

3.6、查看配置是否成功
在地址栏输入:http://http://192.168.230.10/

在地址栏输入:http://192.168.230.10:8088/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MatrixSparse

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

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

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

打赏作者

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

抵扣说明:

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

余额充值