Nginx之虚拟主机
1. 虚拟主机概念和类型介绍
1. 概念
1. 一个server标签就是一个虚拟主机,可以有多个server标签,也就是多个虚拟主机。
2. 一个虚拟主机拥有独立的资源和程序,可以独立地对外提供服务供用户访问。
2. 类型
1. 基于域名的虚拟主机
根据域名来区别不同的虚拟主机,常用于对外提供服务的web网站。
2. 基于端口的虚拟主机
根据端口来区别不同的虚拟主机,常用于内部网站。
3. 基于IP的虚拟主机
根据IP来区别不同的虚拟主机,不常用。
2. 基于域名的虚拟主机
1. 配置文件内容如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.cmr.com.cn;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name learning.cmr.com.cn;
location / {
root html/learning;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2. sername的匹配规则
1. 完整匹配
server_name www.cmr.com.cn
2. 通配符匹配
server_name *.cmr.com.cn
3. 通配符结束匹配
server_name www.cmr.*
4. 正则匹配
server_name ^[0-9]d+\.cmr\.com\.cn$
5. 特殊的匹配
server_name .cmr.com.cn (cmr.com.cn和*.cmr.com.cn合并成一个)
3. 基于主机端口的虚拟主机
1. 配置文件内容如下:
server {
listen 81;
server_name 192.168.0.201;
location / {
root html/learning;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 82;
server_name 192.168.0.201;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4. 基于IP的虚拟主机
1. 配置文件如下:
server {
listen 83;
server_name 192.168.0.201;
location / {
root html/learning;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 83;
server_name 192.168.0.202;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
一往无前虎山行,拨开云雾见光明

浙公网安备 33010602011771号