Nginx---笔记三

3.负载均衡调度器SLB
地域划分:
GSLB
SLB

根据网络模型划分:
四层负载均衡:
四层的负载均衡就是基于IP+端口的负载均衡:在三层负载均衡的基础上,通过发布三层的IP地址(VIP),然后加四层的端口号,来决定哪些流量需要做负载均衡,对需要处理的流量进行NAT处理,转发至后台服务器,并记录下这个TCP或者UDP的流量是由哪台服务器处理的,后续这个连接的所有流量都同样转发到同一台服务器处理。
对应的负载均衡器称为四层交换机(L4 switch),主要分析IP层及TCP/UDP层,实现四层负载均衡。
七层负载均衡:
七层的负载均衡就是基于虚拟的URL或主机IP的负载均衡:在四层负载均衡的基础上(没有四层是绝对不可能有七层的),再考虑应用层的特征,比如同一个Web服务器的负载均衡,除了根据VIP加80端口辨别是否需要处理的流量,还可根据七层的URL、浏览器类别、语言来决定是否要进行负载均衡。
举个例子,如果你的Web服务器分成两组,一组是中文语言的,一组是英文语言的,那么七层负载均衡就可以当用户来访问你的域名时,自动辨别用户语言,然后选择对应的语言服务器组进行负载均衡处理。

对应的负载均衡器称为七层交换机(L7 switch),除了支持四层负载均衡以外,还有分析应用层的信息,如HTTP协议URI或Cookie信息,实现七层负载均衡。此种负载均衡器能理解应用协议,常见例子有: haproxy,MySQL Proxy。
nginx负载均衡:


客户--->Nginx--proxy_pass-->[upstream server
server1,server2,server3...
]
配置语法:
Syntax: upstream name {....}
Default:---
Context:http

示例:

upstream phantom(自定义){
server 192.168.205.10:9090;
server 192.168.205.10:9091;
server 192.168.205.10:9092;
} # 必须配在server之外
server {
listen 80;
server_name phantom.wgw.io;
location / {
proxy_pass http://phantom;
include proxy_params;
}
}
示例:
upstream backend{
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;

server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}

后端服务器在负载均衡调度中的状态:
down:当前的server暂时不参与负载均衡
backup:预留的备份服务器
max_fails:允许请求失败的次数
fail_timeout:经过max_fails失败后,服务暂停的时间
max_conns:限制最大的接收的连接数
upstream phantom{
server 192.168.205.10:9090 down;
server 192.168.205.10:9091 backup;
server 192.168.205.10:9092 max_fails=1 fail_timeout=10s;
}
server {
listen 80;
server_name localhost phantom.wgw.io;
location / {
proxy_pass http://phantom;
include proxy_params;
}
}

正常请求查看只有9092是开着的
关闭9092:
>> iptables -I INPUT -p tcp --dport 9092 -j DROP
查看请求结果,关闭9091:
>> iptables -I INPUT -p tcp --dport 9091 -j DROP
结束,将规则清理掉:
>> iptables -F
再次请求,9092又回来了

nginx调度算法:
轮询 按时间顺序逐一分配到不同的后端服务器
加权轮询 weight值越大,分配到的访问几率越高
ip_hash 每个请求按访问IP的hash结果分配,这样来自同一个IP就会固定访问一个后端服务器
eg:
upstream phantom{
ip_hash;
server 192.168.205.10:9090;
server 192.168.205.10:9091 weight=5;
server 192.168.205.10:9092;
}
url_hash 按访问的URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
配置语法:
Syntax: hash key [consistent];
Default: ---
Context: upstream
This directive appeared in version 1.7.1
eg:
upstream phantom{
hash $request_uri;
server 192.168.205.10:9090;
server 192.168.205.10:9091;
server 192.168.205.10:9092;
}
least_conn 最少链接数,那个机器连接数少就分发
hash关键数值 hash自定义的Key
4.缓存
proxy_cache配置语法:
Syntax:proxy_cache_path path [levels=levels]
[use_temp_path=on|off] keys_zone=name:size[inactive=time]
[max_size=size][manage_files=number][manager_sleep=time]
[manager_threshold=time][loader_files=number]
[loade_sleep=time][loader_threshold=time][purger=on|off]
[purger_files=number][purger_sleep=time]
[purger_threshold=time];
Default:---
Context:http

Syntax: proxy_cache zone|off;
Default: proxy_cache off;
Context: http,server,location

# 缓存过期周期配置
Syntax:proxy_cache_valid[code...]time;
Default:---
Context:http,server,location;

# 缓存维度:
Syntax:proxy_cache_key string;
Default:proxy_cache_key $scheme$proxy_host$request_uri;
Context:http,server,location

eg:

upstream phantom{
server 192.168.205.10:9090;
server 192.168.205.10:9091;
server 192.168.205.10:9092;
}
proxy_cache_path /opt/app/cache(缓存的目录) levels=1:2(按照两层目录的方式进行分级) keys_zone=imooc_cache(自定义):10m max_size(最大)=10g inactive(不活跃的)=
60m(60min) use_temp_path(存放临时文件的建议关闭)=off;

server {
listen 80;
server_name localhost phantom.wgw.io;
location / {
proxy_cache imooc_cache;# 开启缓存和key_zone对应
proxy_pass http://phantom;
proxy_cache_valid 200 304 12h;# 对于返回200,304的头信息对应的过期时间12h
proxy_cache_valid any 10m;# 除200,304以外的10min过期
proxy_cache_key $host$uri$is_args$args;#缓存的key
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;# 当出现error timeout invalid_header http_500 http_502 http_503 http_504就切换到另一台服务器
include proxy_params;
}
如何清理指定缓存:
方式一:rm -rf 缓存目录内容
方式二:第三方扩展模块ngx_cache_purge

如何让页面不缓存:
Syntax: proxy_no_cache string ...;
Default: ---
Contexg: http,server,location

upstream phantom{
server 192.168.205.10:9090;
server 192.168.205.10:9091;
server 192.168.205.10:9092;
}
proxy_cache_path /opt/app/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name localhost phantom.wgw.io;
if ($request_uri ~ ^/(url3|login|register|password\/reset)){
set $cookie_nocache 1;
}
location / {
proxy_cache imooc_cache;
proxy_pass http://phantom;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
proxy_cache_key $host$uri$is_args$args;
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
proxy_no_cache $http_pragma $http_authorization;
add_header Nginx-Cache "$upstream_cache_status";

proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
include proxy_params;
}
}

5.大文件分片请求:
Syntax:slice size;
Default: slice 0;
Context:http,server,location

优势:
每个子请求收到的数据都会形成一个独立文件,一个请求断了,其他请求不受影响

缺点:
当文件很大或者slice很小时,可能导致文件描述符耗尽等情况

posted @ 2019-07-14 20:58  技术小白升职计  阅读(159)  评论(0编辑  收藏  举报