Nginx 构建虚拟主机

虚拟主机

虚拟Web主机指的是在同一台服务器上运行多个Web站点,其中每一个站点实际上并不独自占用整个服务器,因此称为“虚拟”web主机。通过虚拟Web主机服务可以充分利用服务器硬件资源,从而降低网站构建及运行成本。同Apache一样,nginx也配置了3种类型虚拟主机。

Nginx虚拟Web主机分为三种
■ 基于域名:每个虚拟机使用不同域名,相同IP
■ 基于IP:每个虚拟机使用不同域名,且对应不同的IP
■ 基于不同端口号:相同IP,不同TCP端口


备注:1.工作常用到的是基于不同域名的虚拟主机,其他两种虚拟主机运行并不是很多

           2.在构建Apache虚拟主机时,省略了基于ip的虚拟主机实验,在这里特地给大家补上!

 

实验环境

Linux6.5系统

IP地址:192.168.100.10

客户端IP地址:192.168.100.22

yum挂载目录:/mnt/sr0

已搭建Nginx网站服务,参考http://blog.51cto.com/13760351/2160042

实验目标

1.搭建基于IP的虚拟主机

2.搭建基于域名的虚拟主机

3.搭建基于不同端口号的虚拟主机

实验步骤

一、搭建基于不同域名的虚拟主机

1.相同IP 相同端口 不同主机名 benet 和 accp

[root@localhost conf]# vim nginx.conf

server {
        server_name  www.benet.com;    /监听地址
        location / {
            root   /var/www/benet;          /www.benet.com 的工作目录
            index  index.html index.php;
        }
    }
    server {
        server_name  www.accp.com;     /监听地址
        location / {
            root   /var/www/accp;         /www.accp.com 的工作目录
            index  index.html index.php;
        }
    }
}                        /这个括号需要把原文件末尾的括号给去掉

 

[root@localhost conf]# nginx –t

[root@localhost conf]# service nginx.conf

2.分别创建不同目录,并写入不同站点内容

[root@localhost www]# mkdir –p /var/www/html/benet       /创建虚拟用户benet目录
[root@localhost www]# mkdir –p /var/www/html/accp        /创建虚拟用户yun目录

[root@localhost conf.d]# echo “this is benet” > /var/www/html/benet/index.html    /站点写入内容  

[root@localhost conf.d]# echo “this is accp” > /var/www/html/accp/index.html        /写入内容

 

3.搭建DNS服务,更多步骤详见http://blog.51cto.com/13760351/2158118 

[root@localhost conf.d]# service iptables stop        /关闭防火墙
[root@localhost conf.d]# setenforce 0                    /关闭安全性

[root@localhost conf.d]# service samed start        /启动dns域名解析服务

 
4.在客户机上设置dns解析并访问2个域名

1 (2)

5

6

 


二、搭建基于端口的虚拟主机
1.修改nginx配置

 server {
        listen 192.168.100.10:6666;                       /监听6666端口
        server_name  192.168.100.10:6666;           

        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.100.10:8888;                    /监听8888端口
        server_name  192.168.100.10:8888;
}

}

[root@localhost conf]# nginx –t                     /语法检查
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start              /重启服务
[root@localhost conf]# netstat -anpt | grep nginx          /查看端口状态
tcp        0      0 192.168.100.10:8888         0.0.0.0:*               LISTEN      3689/nginx      /8888端口开启
tcp        0      0 192.168.100.10:6666         0.0.0.0:*               LISTEN      3689/nginx       /6666端口开启

 

2.重启服务

[root@localhost conf]# service nginx.conf restart       /重启服务

 

3.客户端访问验证

11

12

三、基于IP的虚拟主机

1.添加双网卡,修改IP

eth0 :192.168.100.100

eth1 :192.168.100.200

 

2.修改配置文件

[root@localhost conf]# vim nginx.conf

server {
         listen 192.168.100.100:80;
        server_name  192.168.100.100:80;
        location / {
            root   /var/www/benet;
            index  index.html index.php;
        }
    }
    server {
        listen 192.168.100.200:80;
        server_name  192.168.100.200:80;
        location / {
            root   /var/www/accp;
            index  index.html index.php;
        }
    }
}


[root@localhost conf]# mkdir -p /var/www/benet  /创建www.benet.com根目录
[root@localhost conf]# mkdir -p /var/www/test   /创建www.benet.com根目录  
[root@localhost conf]# echo "this is benet web " > /var/www/benet/index.html  /写入网页内容

[root@localhost conf]# echo "this is test web " > /var/www/test/index.html   /写入网页内容

[root@localhost conf]# nginx –t                      /语法检查
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start              /重启服务

3.分别访问2个IP地址

15

16

猜你喜欢

转载自blog.51cto.com/13760351/2161517