1.虚拟主机的概念和类型介绍
1.1 虚拟主机概念
所谓虚拟主机,在Web服务里 就是一个独立的网站站点,这个站点对应独立的域名,具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。
这个独立的站点 在配置里由一定的表前段标记的,对于Nginx则使一个Server{}标签来标识一个虚拟主机。一个Web服务里可以有多个虚拟主机标签对,即可以同时支持多个虚拟主机站点。
1.2 虚拟主机的类型
常见的虚拟主机类型由如下几种:
- 基于域名的虚拟主机
所谓基于域名的虚拟主机,意思就是通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服务的网站使用的都是基于域名的虚拟主机,例如:www.baidu.com
- 基于端口的虚拟主机
通过不同的端口来区分不同的虚拟注解,此类虚拟主机对应的企业应用主要为公司内部的网站,例如:一些不希望直接对外提供用户访问的网站后台等,访问基于端口的虚拟主机,地址里也要带有端口
3.基于IP的虚拟主机
通过不同的IP区分不同的虚拟主机,此类虚拟主机对用的企业应该非常少见。一般不同的业务需要使用多IP的场景都会在负载均衡器上进行VIP绑定,而不是在Web上绑定IP来区分不同的虚拟机。
2. 基于域名的虚拟主机配置实战
1.配置基于 域名的nginx.conf内容
vim nginx.conf
worker_processes 1;
events{
worker_connections 1024;
}
http{
include mime.types;
default_type applciation/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
}
配置多个基于域名的虚拟主机
vim nginx.conf
worker_processes 1;
events{
worker_connections 1024;
}
http{
include mime.types;
default_type applciation/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
server {
listen 80;
server_name bbs.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
server {
listen 80;
server_name blog.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
}
2. 基于端口的虚拟主机配置实战
基于端口的虚拟主机在生产环境中不多见,仅偶尔会用到,一般为公司内部人员提供访问,如OA系统、网站程序的后台、CMS发布后台等,使用特殊端口多是从安全上考虑的。
vim nginx.conf
worker_processes 1;
events{
worker_connections 1024;
}
http{
include mime.types;
default_type applciation/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
server {
listen 81;
server_name bbs.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
server {
listen 82;
server_name blog.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
}
3. 基于IP的虚拟主机配置实战
基于IP的虚拟主机在生产环境中的应用更为少见。
worker_processes 1;
events{
worker_connections 1024;
}
http{
include mime.types;
default_type applciation/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 10.0.0.1:80;
server_name www.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
server {
listen 10.0.0.1:81;
server_name bbs.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
server {
listen 10.0.0.1:82;
server_name blog.baidu.com;
location / {
root html1/www;
index index.html index.htm
}
}
}