虚拟主机实战
每个server配置(也就是虚拟主机配置)块通过端口+地址信息进行唯一性校验。
1. 前期准备
sh
[root@hadoop101 ~]# mkdir /www
[root@hadoop101 ~]# mkdir /www/www
[root@hadoop101 ~]# mkdir /www/video
[root@hadoop101 ~]# vim /www/www/index.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h2>这是首页</h2>
</body>
</html>
[root@hadoop101 ~]# vim /www/video/index.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h2>这是Video首页</h2>
</body>
</html>
2. 不同端口配置多个虚拟主机
修改nginx.cnf
ini
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 localhost;
location / {
root /www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 88;
server_name localhost;
location / {
root /www/video;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
运行效果:
2. 不同域名配置多个虚拟主机
- 修改hosts文件:
ini
192.168.101.101 hadoop101 rocket.com mvnhub.com
- 修改nginx.cnf
ini
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 mvnhub.com;
location / {
root /www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name rocket.com;
location / {
root /www/video;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
运行效果:
3. 域名通配符配置多个虚拟主机
- 修改hosts文件:
ini
192.168.101.101 hadoop101 rocket.com mvnhub.com video.mvnhub.com
- 修改nginx.cnf
ini
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 mvnhub.com;
location / {
root /www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name video.mvnhub.com;
location / {
root /www/video;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
运行效果:
4. 域名通配符配置多个虚拟主机
在同一servername中支持匹配多个域名、子域名、域名通配符开始、域名通配符结束和域名正则匹配:
- 修改hosts文件:
ini
192.168.101.101 hadoop101 rocket.com mvnhub.com video.mvnhub.com vd.mvnhub.com mvnhub.org
- 修改nginx.cnf 有两种方式:
ini
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 mvnhub.com;
location / {
root /www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name video.mvnhub.com vd.mvnhub.com;
location / {
root /www/video;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
ini
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 mvnhub.com;
location / {
root /www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name *.mvnhub.com;
location / {
root /www/video;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
ini
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 mvnhub.com;
location / {
root /www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name mvnhub.com.*;
location / {
root /www/video;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
ini
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 mvnhub.com;
location / {
root /www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name ~^[0-9]+\.mmban\.com$;
location / {
root /www/video;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
如果页面加载还是以前的,这是因为缓存导致的,可以多次刷新浏览器或者在url后面加上请求参数 域名通配符匹配开始,运行效果:
域名通配符匹配结束,运行效果:
5. 多用户二级域名
6. 短网址
7. httpdns
7.1 传统DNS域名解析
传统的域名解析是通过UDP协议广播,如下图所示: 常用于网页浏览,由于浏览器本身不保存域名信息特点,每次访问新地址都需要靠外部网络通信才能正确域名解析
7.2 httpdns域名解析
httpdns主要用于app、c/s架构的软件(我们自己的软件可以编码支持记录保存域名信息),用来提高解析速度的。HTTPDNS的工作原理: 比如此时要访问mvnhub.com,需要将其作为参数请求后端得到正式的ip地址。