一、虚拟主机介绍
虚拟主机 就是把一台物理服务器划分成多个“虚拟”的服务器,每一个虚拟主机都可以有独立的域名和独立的目录,可以独立发布一个网站。
实验案例: 同时发布两个网站:
- DocumentRoot /usr/local/nginx/html/web1
- DocumentRoot /usr/local/nginx/html/web2
二、基于IP的虚拟主机
应用场景:IP充足的环境,每个网站需要一个IP地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server { listen 192.168.11.251:80; location / { root html/web1; index index.html index.htm index.php; } } server { listen 192.168.11.252:80; location / { root html/web2; index index.html index.htm; } }
|
基于IP的虚拟主机特点
- 不同IP对应不同网站
- 访问方便,用户直接使用默认端口即可访问
- 服务器需要有多个IP地址(一个公网IP大概一年的费用是600左右)
- 维护方便,基于独立IP的站点,便于监控、维护。
三、基于端口的虚拟主机
应用场景:IP不足的环境
- 优点: 多个网站发布使用该配置方法只需要一个IP,节省IP地址
- 缺点 端口你是无法告诉公网用户,无法适用于公网客户,适合内部用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 基于端口 server { listen 80; #server_name www.test.cc; location / { root html/web1; index index.html index.htm index.php; } } server { listen 8080; #server_name www.test.cc; location / { root html/web2; index index.html index.htm; } }
|
基于端口的虚拟主机特点
- 不同端口对应不同网站
- 访问需要加端口
- 节省IP地址
- 适合私网运行
四、基于域名的虚拟主机
应用场景:一个网站需要有一个域名,目前公网发布网站的首选
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 基于域名 server { listen 80; server_name web1.test.cc;
location / { root html/web1; index index.html index.htm index.php;
} }
server { listen 80; server_name web2.test.cc;
location / { root html/web2; index index.html index.htm; } }
|
基于域名的虚拟主机特点
- 不同域名对应不同网站
- 需要多个域名 可以是二级或三级域名
- 每个站点使用默认端口,方便用户访问
- 只需要一个IP地址,节约成本
- 适合公网环境