Learning基于名称, IP and 端口 虚拟主机configurationmethod, MasterNginx many 网站托管techniques
虚拟主机 (Virtual Host) is 指 in 一台物理server on 托管 many 个网站 techniques. Nginxsupport三种class型 虚拟主机configuration:
虚拟主机techniques使得一台server可以同时托管 many 个网站, improving了server 利用率, reduced运维成本.
基于名称 虚拟主机 is 最常用 虚拟主机configuration方式, 它through不同 域名来区分不同 网站.
through in server块in设置不同 server_name指令来implementation基于名称 虚拟主机:
# 第一个虚拟主机
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
# 第二个虚拟主机
server {
listen 80;
server_name test.com www.test.com;
root /var/www/test.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
server_name指令support以 under 匹配规则:
example.com*.example.com or mail.*~^www\.(.+)$# 精确匹配
server {
listen 80;
server_name example.com;
# configuration in 容
}
# 通配符匹配
server {
listen 80;
server_name *.example.com;
# configuration in 容
}
# 正则表达式匹配
server {
listen 80;
server_name ~^www\.(.+)$;
# configuration in 容
}
基于IP 虚拟主机through不同 IP地址来区分不同 网站. 这种方式需要server has many 个IP地址.
through in listen指令in指定不同 IP地址来implementation基于IP 虚拟主机:
# 第一个IP地址 虚拟主机
server {
listen 192.168.1.100:80;
root /var/www/site1;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
# 第二个IP地址 虚拟主机
server {
listen 192.168.1.101:80;
root /var/www/site2;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
基于端口 虚拟主机through不同 端口来区分不同 网站. 这种方式适用于testenvironment or 不需要域名 场景.
through in listen指令in指定不同 端口来implementation基于端口 虚拟主机:
# 80端口 虚拟主机
server {
listen 80;
root /var/www/site1;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
# 8080端口 虚拟主机
server {
listen 8080;
root /var/www/site2;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
当 many 个虚拟主机configuration可能匹配同一个request时, Nginx会按照以 under priority顺序选择虚拟主机:
such asserver_name example.com;
such asserver_name *.example.com;
such asserver_name mail.*;
such asserver_name ~^www\.(.+)$;
第一个匹配 to listen端口 虚拟主机, or 显式设置 for default_server 虚拟主机
可以through in listen指令in添加default_serverparameter来设置默认虚拟主机:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}
这个configuration会拒绝所 has 不匹配other虚拟主机 request.
以 under is a configuration many 个虚拟主机 完整example:
# 默认虚拟主机
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}
# 第一个网站: example.com
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm;
access_log /var/log/nginx/example.com.access.log main;
error_log /var/log/nginx/example.com.error.log warn;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
}
# 第二个网站: test.com
server {
listen 80;
server_name test.com www.test.com;
root /var/www/test.com;
index index.html index.htm;
access_log /var/log/nginx/test.com.access.log main;
error_log /var/log/nginx/test.com.error.log warn;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
}
# 第三个网站: through8080端口访问
server {
listen 8080;
root /var/www/dev;
index index.html index.htm;
access_log /var/log/nginx/dev.access.log main;
error_log /var/log/nginx/dev.error.log warn;
location / {
try_files $uri $uri/ =404;
}
}
这个configurationpackage含了:
/etc/nginx/conf.d/Table of Contentsin/var/www/example.com1. Nginxsupport哪些class型 虚拟主机? ( many 选)
2. 以 under 哪种虚拟主机匹配规则priority最 high ?
3. 实践练习: configuration两个基于名称 虚拟主机
请configuration两个基于名称 虚拟主机:
through本tutorial, 您已经Learning了Nginx虚拟主机 configurationmethod, including:
虚拟主机 is Nginx important functions, 它使得一台server可以同时托管 many 个网站, improving了server 利用率. in after 续 tutorialin, 我们将深入LearningNginx 反向proxy, load balancing, 静态fileservice, securityconfiguration, performanceoptimizationetc.advanced features, helping您全面MasterNginx usingtechniques.