0
点赞
收藏
分享

微信扫一扫

Nginx 虚拟主机

沪钢木子 2022-03-12 阅读 64


一、虚拟主机介绍

就是把一台物理服务器划分成多个“虚拟”的服务器,每一个虚拟主机都可以有独立的域名和独立的目录

同时发布两个网站:

DocumentRoot /usr/local/nginx/html/web1

DocumentRoot /usr/local/nginx/html/web2

二、基于IP的虚拟主机

应用场景:IP充足的环境

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的虚拟主机特点


  1. 不同IP对应不同网站
  2. 访问方便,用户直接使用默认端口即可访问
  3. 服务器需要有多个IP地址(一个公网IP大概一年的费用是600左右)
  4. 维护方便,基于独立IP的站点,便于监控、维护。

三、基于端口的虚拟主机

只需要一个IP

缺点:端口你是无法告诉公网用户 无法适用于公网客户 适合内部用户。

基于端口
server {
listen 80;
#server_name www.abc.com;
location / {
root html/web1;
index index.html index.htm index.php;
}
}
server {
listen 8080;
#server_name www.abc.com;
location / {
root html/web2;
index index.html index.htm;
}
}

基于端口的虚拟主机特点


  1. 不同端口对应不同网站
  2. 访问需要加端口
  3. 节省IP地址
  4. 适合私网运行

四、基于域名的虚拟主机

一个网站必然有一个域名

基于域名
server {
listen 80;
server_name web1.ayitula.com;

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


server {
listen 80;
server_name web2.ayitula.com;

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

基于域名的虚拟主机特点


  1. 不同域名对应不同网站
  2. 需要多个域名 可以是二级或三级域名
  3. 每个站点使用默认端口,方便用户访问
  4. 只需要一个IP地址,节约成本
  5. 适合公网环境


举报

相关推荐

0 条评论