Nginx基于IP,端口,域名配置虚拟主机

Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。Nginx同Apache httpd一样,Nginx也提供基于IP,基于端口以及域名方式的形式来配置虚拟主机。

一、什么是虚拟主机

虚拟主机是使用特殊的软硬件技术,把一台真实的物理服务器主机分割成多个逻辑存储单元。每个逻辑单元都没有物理实体,但是每一个逻辑单元都能像真实的物理主机一样在网络上工作,具有单独的IP地址(或共享的IP地址)、独立的域名以及完整的Internet服务器(支持WWW、FTP、E-mail等)功能。

虚拟主机的关键技术在于,即使在同一台硬件、同一个操作系统上,运行着为多个用户打开的不同的服务器程式,也互不干扰。而各个用户拥有自己的一部分系统资源(IP地址、文档存储空间、内存、CPU等)。各个虚拟主机之间完全独立,在外界看来,每一台虚拟主机和一台单独的主机的表现完全相同。所以这种被虚拟化的逻辑主机被形象地称为“虚拟主机”。

二、基于端口的虚拟主机

1、准备环境
#当前环境
# more /etc/issue
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Kernel \r on an \m

# uname -rm
2.6.32-279.el6.x86_64 x86_64

# nginx -v
nginx version: nginx/1.8.0

# 创建3个目录用于存放不同形式虚拟主机index.html文件
# mkdir -p /website/baseport
# mkdir -p /website/baseip
# mkdir -p /website/basedomain

# vi /website/baseport/index.html 
<!DOCTYPE html>
<html>
<head>
<title>Base port sample</title>
</head>
<body>
<h1>This is an based port website sample(prot:8080).</h1>
</body>
</html>

2、配置nginx.conf
#第一个虚拟主机
server {
        listen       80;
        server_name  localhost;

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

#第二个虚拟主机        
server {       
        listen       8080;
        server_name  localhost;

        location / {
            root   /website/port;
            index  index.html index.htm;
        }
    }

3、验证   
# nginx -t              #语法检查
# service nginx reload  #服务重载
# curl http://192.168.1.120:8080  #验证基于端口访问
<!DOCTYPE html>
<html>                                 
<head>
<title>Base port sample</title>
</head>
<body>
<h1>This is an based port website sample(prot:8080).</h1>
</body>
</html>

三、基于IP的虚拟主机

1、先添加IP
# ifconfig|grep "inet addr"
          inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0
# ifconfig eth0:0 192.168.1.220 netmask 255.255.255.0 up  #添加IP到eth0:0
# ifconfig|grep "inet addr"
          inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:192.168.1.220  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0

2、配置nginx.conf
#第一个虚拟主机
server {
        listen       80;
        server_name  localhost;

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

#第二个虚拟主机                      
 server {
        listen       192.168.1.220:80;
        server_name  localhost;

        location / {
            root   /website/baseip;
            index  index.html index.htm;
        }
    }

3、验证    
# nginx -t                     #语法检查      Author:Leshami                     
# service nginx reload         #服务重载      Blog  :http://blog.csdn.net/leshami
# curl http://192.168.1.220    #验证基于IP访问
<!DOCTYPE html>
<html>
<head>
<title>Base ip sample</title>
</head>
<body>
<h1>This is an based ip website sample.</h1>
</body>
</html>

四、基于域名的虚拟主机

1、修改/etc/hosts文件
# echo "
192.168.1.120 bbs.ycdata.net bbs
192.168.1.120 mail.ycdata.net mail
> ">>/etc/hosts

2、配置nginx.conf
#第一个虚拟主机
server {
        listen       80;
        server_name mail.ycdata.net;

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

#第二个虚拟主机        
server {
        listen       80;
        server_name  bbs.ycdata.net;

        location / {
            root   /website/baseport;
            index  index.html index.htm;
        }
    }

3、验证
# curl http://mail.ycdata.net
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

# curl http://bbs.ycdata.net
<!DOCTYPE html>
<html>
<head>
<title>Base port sample</title>
</head>
<body>
<h1>This is an based port website sample(prot:8080).</h1>
</body>
</html>
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值