您的位置:首页 > 运维架构 > Nginx

Nginx实现虚拟主机

2015-03-30 17:34 162 查看
一、何为虚拟主机?
单个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;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  虚拟主机 nginx