码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx实现虚拟主机

时间:2015-03-31 01:11:58      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:nginx   虚拟主机   

一、何为虚拟主机?

     单个HTTPserver能提供多个http服务,根据访问的方式,可以分为以下几种虚拟主机:

                1.端口虚拟主机

                2.IP虚拟主机

                3.域名虚拟主机


二、配置实现

    1.端口虚拟主机

   .......
    http { 
    ....
    server {
        listen 8080;        # 这里设置虚拟主机的监听端口
        server_name virtual_host1;
        location / {
           root  /var/vhost/vhost1;      #vhost1的首页位置
           index index.html  index.htm;
        }

    }
        server {
        listen 80;          #这里设计虚拟主机的监听端口
        server_name virtual_host2;
        location / {
           root  /var/vhost/vhost2;         #vhost2的首页位置   
           index index.html  index.htm;
        }

    }
  }

    2.IP虚拟主机(IP1,IP2需要在服务器上)

  .......
    http { 
    ....
    server {
        listen IP1:80;        # 这里设置虚拟主机的监听端口
        server_name virtual_host1;
        location / {
           root  /var/vhost/vhost1;      #vhost1的首页位置
           index index.html  index.htm;
        }

    }
        server {
        listen IP2:80;          #这里设计虚拟主机的监听端口
        server_name virtual_host2;
        location / {
           root  /var/vhost/vhost2;         #vhost2的首页位置   
           index index.html  index.htm;
        }

    }
  }

    3.域名虚拟主机

.......
    http { 
    ....
    server {
        listen IP1:80;        
        server_name    #虚拟域名  
        location / {
           root  /var/vhost/vhost1;      #vhost1的首页位置
           index index.html  index.htm;
        }

    }
        server {
        listen IP2:80;         
        server_name     #虚拟域名 
        location / {
           root  /var/vhost/vhost2;         #vhost2的首页位置   
           index index.html  index.htm;
        }

    }
  }



本文出自 “Lu2Yu” 博客,请务必保留此出处http://lu2yu.blog.51cto.com/10009517/1626497

Nginx实现虚拟主机

标签:nginx   虚拟主机   

原文地址:http://lu2yu.blog.51cto.com/10009517/1626497

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!