目录

虚拟主机与域名解析

# 虚拟主机与域名解析

  • 域名、dns、ip地址的关系
  • 浏览器、Nginx与http协议
  • 虚拟主机原理
  • 域名解析与泛域名解析实战
  • 域名解析相关企业项目实战技术架构
    • 多用户二级域名
    • 短网址
    • HTTP DNS
  • Nginx中的虚拟主机配置

# 浏览器、Nginx与HTTP协议

【尚硅谷】2022版Nginx教程(nginx入门到亿级流量)_哔哩哔哩_bilibili (opens new window)

# 多二级域名

有时,我们需要建立不同的虚拟主机管理不同的网站请求,因此我们可以创建多个虚拟主机进行管理。 值得注意的是server_name是可以接受IP、域名、甚至正则表达式的

# 虚拟主机  
server {  
    # 监听端口号  
    listen       80;  
    # 服务器IP  
    server_name  localhost;  
  
    location / {  
  
        # 页面资源的存放目录,这里是../html下  
        root   /www/www;  
        index  index.html index.htm;  
    }  
    # 当响应是500 502 503 504时 返回50x.html页面  
    error_page   500 502 503 504  /50x.html;  
    # 当用户访问50x.html找不到时,会去../html目录下找  
    location = /50x.html {  
        root   html;  
    }  
}  
# 虚拟主机2  
server {  
    # 监听端口号  
    listen       88;  
    # 服务器IP  
    server_name  localhost;  
  
    location / {  
  
        # 页面资源的存放目录,这里是../html下  
        root   /www/vod;  
        index  index.html index.htm;  
    }  
    # 当响应是500 502 503 504时 返回50x.html页面  
    error_page   500 502 503 504  /50x.html;  
    # 当用户访问50x.html找不到时,会去../html目录下找  
    location = /50x.html {  
        root   html;  
    }  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# 反向代理

以下是最简单的反向代理,但由于CA证书缘故,反向代理不支持HTTPS

location / {  
    proxy_pass http://www.baidu.com;  
    # 页面资源的存放目录,这里是../html下  
    #root   html;  
    #index  index.html index.htm;
    }
1
2
3
4
5
6

多服务器反向代理

http {  
    # 决定返回文件的类型,例如: jpg, html, css...  
    # mime.types可以查看conf/mime.types  
    include       mime.types;  
    # 如果不包含在mime.types里,则默认为application/octet-stream  
    default_type  application/octet-stream;  
  
    sendfile        on;  
    # 响应超时范围  
    keepalive_timeout  65;  
  
    upstream httpds {  
        server 192.168.229.134:80;  
    }  
    # 虚拟主机  
    server {  
        # 监听端口号  
        listen       80;  
        # 服务器IP  
        server_name  localhost;  
  
        location / {  
            proxy_pass http://httpds;  
            # 页面资源的存放目录,这里是../html下  
            #root   html;  
            #index  index.html index.htm;        }  
        # 当响应是500 502 503 504时 返回50x.html页面  
        error_page   500 502 503 504  /50x.html;  
        # 当用户访问50x.html找不到时,会去../html目录下找  
        location = /50x.html {  
            root   html;  
        }  
    }  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# 负载均衡

# 轮询权重

![[Pasted image 20220519185326.png]] 假设我们有两台后台服务器,这两台服务器的带宽不一,一个是1000M,另一个是100M,我们希望1000M的服务器能负责8次服务,100M的服务器负责2次服务。那么我们该如何设置呢?

upstream httpds {  
    server 192.168.229.134:80 weight=8;  
    server 192.168.229.135:80 weight=2;
}
1
2
3
4
# down

那么问题来了,我们给134配置的权重过大导致他超出我们预期的负荷,此时我们希望他能“休息一下”该如何实现呢?

upstream httpds {  
    server 192.168.229.134:80 weight=8 down;  
    server 192.168.229.135:80 weight=2;
}
1
2
3
4
# backup

你搭建的电商平台在双十一访问量爆炸式增长,你打算设置一个备用服务器在高负载情况下响应请求,该如何做到呢?

upstream httpds {  
    server 192.168.229.134:80 weight=8 down;  
    server 192.168.229.135:80 weight=2;
    server 192.168.229.136:80 backup;
}
1
2
3
4
5

被设置backup的服务器只会在其余服务器皆不可用时才能被访问,因此成为了备用服务器。 但实际场景中,down和backup都不常用,既然你已经知道服务器down了,你可以重启而不是备注一个down标签。

此外,上述weight、down、backup属于轮询策略,无法保持会话。

最近更新
01
基本知识
07-18
02
卷积神经网络识别图像
07-18
03
损失函数
07-18
更多文章>