lvs调度算法和nginx常用模块

1、简述LVS调度方案及应用场景
2、详细描述nginx模块并举例说明

1、简述LVS调度方案及应用场景

lvs的调度算法:

静态方法:仅根据算法本身进行调度;
    RR:roundrobin,轮询;
    WRR:Weighted RR,加权轮询;
    SH:Source Hashing,实现session sticky,源IP地址hash;将来自于同一个IP地址的请求始终发往第一次挑中的RS,从而实现会话绑定;
    DH:Destination Hashing;目标地址哈希,将发往同一个目标地址的请求始终转发至第一次挑中的RS,典型使用场景是正向代理缓存场景中的负载均衡;

动态方法:主要根据每RS当前的负载状态及调度算法进行调度;
    Overhead=

    LC:least connections
        Overhead=activeconns*256+inactiveconns
    WLC:Weighted LC
        Overhead=(activeconns*256+inactiveconns)/weight
    SED:Shortest Expection Delay
        Overhead=(activeconns+1)*256/weight
    NQ:Never Queue
    
    LBLC:Locality-Based LC,动态的DH算法;
    LBLCR:LBLC with Replication,带复制功能的LBLC;

lvs集群的类型:

  • lvs-nat:修改请求报文的目标IP;多目标IP的DNAT;
  • lvs-dr:操纵封装新的MAC地址;
  • lvs-tun:在原请求IP报文之外新加一个IP首部;
  • lvs-fullnat:修改请求报文的源和目标IP;

lvs-nat:
多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发;

    (1)RIP和DIP必须在同一个IP网络,且应该使用私网地址;RS的网关要指向DIP;
    (2)请求报文和响应报文都必须经由Director转发;Director易于成为系统瓶颈;
    (3)支持端口映射,可修改请求报文的目标PORT;
    (4)vs必须是Linux系统,rs可以是任意系统;

lvs-dr:
通过为请求报文重新封装一个MAC首部进行转发,源MAC是DIP所在的接口的MAC,目标MAC是某挑选出的RS的RIP所在接口的MAC地址;源IP/PORT,以及目标IP/PORT均保持不变;

Director和各RS都得配置使用VIP
(1) 确保前端路由器将目标IP为VIP的请求报文发往Director:
    (a) 在前端网关做静态绑定;
    (b) 在RS上使用arptables;
    (c) 在RS上修改内核参数以限制arp通告及应答级别;
        arp_announce
        arp_ignore
(2) RS的RIP可以使用私网地址,也可以是公网地址;RIP与DIP在同一IP网络;RIP的网关不能指向DIP,以确保响应报文不会经由Director;
(3) RS跟Director要在同一个物理网络;
(4) 请求报文要经由Director,但响应不能经由Director,而是由RS直接发往Client;
(5) 不支持端口映射;

lvs-tun:
转发方式:不修改请求报文的IP首部(源IP为CIP,目标IP为VIP),而是在原IP报文之外再封装一个IP首部(源IP是DIP,目标IP是RIP),将报文发往挑选出的目标RS;RS直接响应给客户端(源IP是VIP,目标IP是CIP);

(1) DIP, VIP, RIP都应该是公网地址;
(2) RS的网关不能,也不可能指向DIP;
(3) 请求报文要经由Director,但响应不能经由Director;
(4) 不支持端口映射;
(5) RS的OS得支持隧道功能;

lvs-fullnat:
通过同时修改请求报文的源IP地址和目标IP地址进行转发;

(1) VIP是公网地址,RIP和DIP是私网地址,且通常不在同一IP网络;因此,RIP的网关一般不会指向DIP;
(2) RS收到的请求报文源地址是DIP,因此,只能响应给DIP;但Director还要将其发往Client;
(3) 请求和响应报文都经由Director;
(4) 支持端口映射;

注意:此类型默认不支持;

总结:

lvs-nat, lvs-fullnat:请求和响应报文都经由Director;
    lvs-nat:RIP的网关要指向DIP;
    lvs-fullnat:RIP和DIP未必在同一IP网络,但要能通信;
lvs-dr, lvs-tun:请求报文要经由Director,但响应报文由RS直接发往Client;
    lvs-dr:通过封装新的MAC首部实现,通过MAC网络转发;
    lvs-tun:通过在原IP报文之外封装新的IP首部实现转发,支持远距离通信;

2、详细描述nginx模块并举例说明

1、ngx_http_access_module模块:
实现基于ip的访问控制功能

        location / {
        allow 192.168.31.204/32;
        deny all;
        }

结果:

curl http://192.168.31.200
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

curl http://192.168.31.200
<h1>The node1 Server</h1>

2、ngx_http_auth_basic_module模块
实现基于用户的访问控制,使用basic机制进行用户认证;
注意:htpasswd命令由httpd-tools所提供;
语法:

  • auth_basic string | off;设置为off可以不继承上一级的basic配置
  • auth_basic_user_file file;指定保存用户名密码的文件

步骤:
创建用户密码文件,注意第一次创建加上-c以后就不需要加了,否则会覆盖原来的文件。
方法1、htpasswd -cb /etc/nginx/conf.d/.htpasswd lvqing 123456
方法2、echo "lvqing:$(openssl passwd -crypt 123456)" >> /etc/nginx/conf.d/.htpasswd
再在配置文件中添加

location / {
    auth_basic           "admin passwd";
    auth_basic_user_file passwd/.htpasswd;
}

3、ngx_http_stub_status_module模块
用于输出nginx的基本状态信息;
直接在配置文件中添加

location  /basic_status {
        stub_status;
}

结果:

curl http://192.168.31.200/basic_status
Active connections: 1
server accepts handled requests
 24 24 24
Reading: 0 Writing: 1 Waiting: 0

Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数

4、ngx_http_log_module模块
用指定的格式写入请求日志
语法:
log_format name string ...; string可以使用nginx核心模块及其它模块内嵌的变量;

access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];访问日志文件路径

  • access_log off;
  • buffer=size:缓冲大小
  • flush=time :日志滚动的时间

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;缓存日志文件的元数据

  • max:缓存的最大文件描述符数量;
  • min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项;
  • inactive:非活动时长;
  • valid:验正缓存中各缓存项是否为活动项的时间间隔;

示例:

'$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$gzip_ratio" "$http_x_forwarded_for"';

access_log  /var/log/nginx/logtest/nginx-access.log compression buffer=32k;

结果:

tail /var/log/nginx/logtest/nginx-access.log
192.168.31.204-lvqing[30/Dec/2018:11:59:41 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"
192.168.31.204-lvqing[30/Dec/2018:11:59:41 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"
192.168.31.205--[30/Dec/2018:11:59:01 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"
192.168.31.204-lvqing[30/Dec/2018:11:59:38 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"
192.168.31.204-lvqing[30/Dec/2018:11:59:39 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"
192.168.31.205--[30/Dec/2018:12:00:27 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"
192.168.31.205--[30/Dec/2018:12:00:27 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"
192.168.31.205--[30/Dec/2018:12:00:28 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"
192.168.31.205--[30/Dec/2018:11:58:59 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"
192.168.31.204-lvqing[30/Dec/2018:11:59:40 +0800] "GET / HTTP/1.1" 200 262 "-" "curl/7.29.0" "-"

5、ngx_http_gzip_module模块
用gzip算法来压缩数据可以节约带宽,默认nginx不压缩数据,但是压缩会消耗CPU资源,且压缩文本图像类效果较好,能达到30%左右,但压缩音频视频没有多大意义,因为本身音视频就是被压缩过的,很可能对音视频压缩反而会增大其体积。
语法:

1、gzip on | off;
        Enables or disables gzipping of responses.
                        
2、gzip_comp_level level;
        Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9.
                        
3、  gzip_disable regex ...;
        Disables gzipping of responses for requests with “User-Agent” header fields matching any of the specified regular expressions.
                        
4、  gzip_min_length length;
        启用压缩功能的响应报文大小阈值; 
                        
5、gzip_buffers number size;
        支持实现压缩功能时为其配置的缓冲区数量及每个缓存区的大小;
                        
6、gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
        nginx作为代理服务器接收到从被代理服务器发送的响应报文后,在何种条件下启用压缩功能的;
            off:对代理的请求不启用
            no-cache, no-store,private:表示从被代理服务器收到的响应报文首部的Cache-Control的值为此三者中任何一个,则启用压缩功能;
                            
7、gzip_types mime-type ...;
        压缩过滤器,仅对此处设定的MIME类型的内容启用压缩功能;

配置示例:

gzip  on;
gzip_comp_level 6;
gzip_min_length 64;
gzip_proxied any;
gzip_types text/xml text/css  application/javascript;   

结果:


13920922-68cbdd5aba7f3184.png
文本数据原来大小是465B压缩后只有180B
13920922-b96e90dd5b38fd1a.png
而图像压缩比例更惊人

6、ngx_http_ssl_module模块
启用https时使用的模块
语法:

1、ssl on | off;
    Enables the HTTPS protocol for the given virtual server.
                        
2、ssl_certificate file;
    当前虚拟主机使用PEM格式的证书文件;
                        
3、ssl_certificate_key file;
    当前虚拟主机上与其证书匹配的私钥文件;
                        
4、ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
    支持ssl协议版本,默认为后三个;
                        
5、ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
    builtin[:size]:使用OpenSSL内建的缓存,此缓存为每worker进程私有;
    [shared:name:size]:在各worker之间使用一个共享的缓存;
                        
6、ssl_session_timeout time;
    客户端一侧的连接可以复用ssl session cache中缓存 的ssl参数的有效时长;

当然因为用到了ssl所以也需要CA服务器,可以将nginx服务器自己作CA签发证书给自己和客户端。
配置示例:

server{
        listen 443 ssl;
        server_name node1.lvqing.com;
        root /var/nginx/www/;
        access_log /var/log/nginx/ngx_ssl_access.log main;

        ssl on;
        ssl_certificate /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;
        ssl_protocols sslv3 tlsv1 tlsv1.1 tlsv1.2;
        ssl_session_cache shared:SSL:10m; #打开会话缓存
}

7、ngx_http_rewrite_module模块
重定向模块,可以将客户端的请求基于regex所描述的模式进行检查,而后完成替换。当旧的业务和新的业务不一样,网站更名了,就可以使用此模块。将访问旧的请求重定向成新的。
语法:

1、rewrite regex replacement [flag]
    将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI;
    
    注意:如果在同一级配置块中存在多个rewrite规则,那么会自上而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查,因此,隐含有循环机制;[flag]所表示的标志位用于控制此循环机制,但不超过10次;如果超过,会给客户端一个500响应码;
  redirect:临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;不能以http://或https://开头,使用相对路径,状态码:302
  replacement:永久重定向是以http://或https://开头,则替换结果会直接以重向返回给客户端,状态码 :301

    [flag]:
        last:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URI启动新一轮重写检查;提前重启新一轮循环; 
        break:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环;
          last 和break是在服务器内部操作的,客户端不知道,客户端访问的url不会发生变化。但是服务器端会返回替换过的新的内容。
        redirect:重写完成后以临时重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;不能以http://或https://开头;
        permanent:重写完成后以永久重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;
          redirect和permanent是服务器端给客户端发一个301或者302的请求,客户端需要重新发起请求,因此最终客户端看到的浏览器是url和原始的url是不一样的,url会被转换新指定的。
2、return
    return code [text];
    return code URL;
    return URL;
    
    停止处理并将指定的代码返回给客户端。 
    
3、  rewrite_log on | off;
    是否开启重写日志;
    
4、  if (condition) { ... }
    引入一个新的配置上下文 ;条件满足时,执行配置块中的配置指令;server, location;
    
    condition:
        比较操作符:
            ==
            !=
            ~:模式匹配,区分字符大小写;
            ~*:模式匹配,不区分字符大小写;
            !~:模式不匹配,区分字符大小写;
            !~*:模式不匹配,不区分字符大小写;
        文件及目录存在性判断:
            -e, !-e
            -f, !-f
            -d, !-d
            -x, !-x
            
5、set $variable value;
    用户自定义变量 ;

示例:

rewrite /(.*)\.png$/$1.jpg;

将所有请求以png结尾的重定向以jpg结尾的

8、ngx_http_referer_module模块
可以跟踪链接从哪里跳转过来的,该字段可以用来防止盗链
语法:

valid_referers none | blocked | server_names | string ...;
    定义referer首部的合法可用值;
                    
        none:请求报文首部没有referer首部;
        blocked:请求报文的referer首部没有值;
        server_names:参数,其可以有值作为主机名或主机名模式;
        arbitrary_string:直接字符串,但可使用*作通配符;
        regular expression:被指定的正则表达式模式匹配到的字符串;要使用~打头,例如 ~.*\.lvqing\.com;

示例:

 valid_referers none block server_names *.lvqing.com *.lvqing.com *.lvqing.* ~.*\.lvqing\.com;
        if($invalid_referer) {
                return http://node1.lvqing.com/background.jpg;
        }
#定义如果出现无效的referer将返回code 403
#只用从匹配到的地址跳转过来的请求才允许访问return的地址

nginx的代理功能所使用到的模块

9、ngx_http_headers_module模块
向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值
语法:

1、add_header name value [always];
    添加自定义首部;
    
    add_header X-Via  $server_addr;
    add_header X-Accel $server_name;
    
2、expires [modified] time;
    expires epoch | max | off;
    
    用于定义Expire或Cache-Control首部的值;

示例:

add_header proxy_name lvqing_proxy;
add_header lvqing_X-Via  $server_addr;
add_header lvqing_X-Accel  $server_name;

13920922-c6e51a77b88fed3b.png
image.png

10、ngx_http_proxy_module模块
这个没什么好说的,没这个nginx就无法代理转发请求
语法:

1、proxy_pass URL;
    Context:    location, if in location, limit_except
    
    注意:proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机;
        
        server {
            ...
            server_name HOSTNAME;
            location /uri/ {
                proxy_pass  http://hos[:port];
            }
            ...
        }
        
        http://HOSTNAME/uri --> http://host/uri 
        
    proxy_pass后面的路径是一个uri时,其会将location的uri替换为proxy_pass的uri;
        
        server {
            ...
            server_name HOSTNAME;
            location /uri/ {
                proxy_pass http://host/new_uri/;
            }
            ...
        }
        
        http://HOSTNAME/uri/ --> http://host/new_uri/
        
    如果location定义其uri时使用了正则表达式的模式,或在if语句或limt_execept中使用proxy_pass指令,
则proxy_pass之后必须不能使用uri; 用户请求时传递的uri将直接附加代理到的服务的之后;
    
        server {
            ...
            server_name HOSTNAME;
            location ~|~* /uri/ {
                proxy http://host;
            }
            ...
        }
        
        http://HOSTNAME/uri/ --> http://host/uri/;
        
2、proxy_set_header field value;
    设定发往后端主机的请求报文的请求首部的值;Context:   http, server, location
       proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        $proxy_add_x_forwarded_for:经过了哪些主机的代理使用,隔开
        $remote_addr:代表了客户端的IP,可能是最后一个代理服务器的地址

3、proxy_cache_path
    定义可用于proxy功能的缓存;Context:    http            
    
    proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] 
[loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
    

4、proxy_cache zone | off;
    指明要调用的缓存,或关闭缓存机制;Context:   http, server, location
    
5、  proxy_cache_key string;
    缓存中用于“键”的内容;
    
    默认值:proxy_cache_key $scheme$proxy_host$request_uri;
    
6、proxy_cache_valid [code ...] time;
    定义对特定响应码的响应内容的缓存时长;
    
    定义在http{...}中;
    proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;
    
    定义在需要调用缓存功能的配置段,例如server{...}或location{....};
    proxy_cache pxycache;
    proxy_cache_key $request_uri;
    proxy_cache_valid 200 302 301 1h;    定义不同类的内容缓存多长时间
    proxy_cache_valid any 1m;
    
7、proxy_cache_use_stale
    
    proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;
    
    Determines in which cases a stale cached response can be used when an error occurs during communication with the proxied server.
    
8、proxy_cache_methods GET | HEAD | POST ...;
    If the client request method is listed in this directive then the response will be cached. 
“GET” and “HEAD” methods are always added to the list, though it is recommended to specify them explicitly. 
    
9、proxy_hide_header field;
    By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-...” 
from the response of a proxied server to a client. The proxy_hide_header directive sets additional fields that will not be passed.
    
10、proxy_connect_timeout time;
    Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.
    建立链接服务器端超时
    默认为60s;最长为75s;
    
11、proxy_read_timeout time; 
    Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations, not for the transmission of the whole response.
    接收服务端响应超时
12、proxy_send_timeout time;
    Sets a timeout for transmitting a request to the proxied server. he timeout is set only between two successive write operations, 
not for the transmission of the whole request. If the proxied server does not receive anything within this time, the connection is closed.
    向服务端发请求超时           

示例:
所有发给node1以php结尾的请求都代理给后端的node2服务器,同时调用缓存pxycache,以proxy_host和request_uri做cache的建

proxy.conf
server{
        server_name node1.lvqing.com;
        location / {
        root /var/nginx/www/;
        }
        location ~* \.php$ {
                proxy_cache pxycache;
                proxy_cache_key $proxy_host$request_uri;
                proxy_pass http://node2.lvqing.com;
        }
}

nginx.conf
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;

同时在node2的默认目录下创建一个php测试文件
index.php
<h1>node2 Server</h2>
<?php
        phpinfo()
?>

结果:
成功代理到node2上

[root@node1 bak]# curl http://node1.lvqing.com/index.php
<h1>node2 Server</h2>
<?php
        phpinfo()
?>

11、ngx_http_fastcgi_module
该模块允许将请求传递给FastCGI服务器。可以认为这个模块提供了nginx与web服务器上的应用交流的接口。有了这个模块nginx就可以将请求转发给web服务器上的php或者tomcat等应用程序,形成动态网站。
语法:


1、fastcgi_pass address;
    address为fastcgi server的地址;  location, if in location;
    
        http://www.ilinux.io/admin/index.php --> /admin/index.php (uri)
            /data/application/admin/index.php
                
2、fastcgi_index name;
    fastcgi默认的主页资源; 
    
3、fastcgi_param parameter value [if_not_empty];
    Sets a parameter that should be passed to the FastCGI server.
 The value can contain text, variables, and their combination.
    
        
4、fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] 
[loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

    定义fastcgi的缓存;缓存位置为磁盘上的文件系统,由path所指定路径来定义;
    
        levels=levels:缓存目录的层级数量,以及每一级的目录数量;levels=ONE:TWO:THREE
            leves=1:2:2
        keys_zone=name:size
            k/v映射的内存空间的名称及大小
        inactive=time
            非活动时长
        max_size=size
            磁盘上用于缓存数据的缓存空间上限

        
5、fastcgi_cache zone | off;
    调用指定的缓存空间来缓存数据;http, server, location
    
6、fastcgi_cache_key string;
    定义用作缓存项的key的字符串;
    
7、fastcgi_cache_methods GET | HEAD | POST ...;
    为哪些请求方法使用缓存;
    
8、fastcgi_cache_min_uses number;
    缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项;
    
9、fastcgi_cache_valid [code ...] time;
    不同的响应码各自的缓存时长;
    
10、fastcgi_keep_conn on | off;
        By default, a FastCGI server will close a connection right after sending the response. However, when this directive is set to the value on, nginx will instruct a FastCGI server to keep connections open.
        默认情况下,FastCGI服务器会在发送响应后立即关闭连接。然而,当这个指令被设置为on时,nginx会指示FastCGI服务器保持连接打开。

示例:
node2配置文件

fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20m inactive=120s;

server{
        server_name node2.lvqing.com;
        location ~* \.php$ {
                root /var/nginx/www;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                #这里的$document_root指的是上面root指定的家目录,意在解释这个目录内的php脚本。
                include        fastcgi_params;
                fastcgi_cache fcgi;
                fastcgi_cache_key $request_uri;
                fastcgi_cache_valid 200 302 10m;
                fastcgi_cache_valid 301 1h;
                fastcgi_cache_valid any 1m;
        }
}

结果

13920922-b23b0f18b1066b6c.png

12、ngx_http_upstream_module
将后端的多个服务器定义成服务器组,对代理的请求做基于七层的负载均衡,自带健康状态检测功能,可由proxy_pass,fastcgi_pass,uwsgi_pass,scgi_pass和memcached_pass指令引用的服务器组
负载均衡设备如果要根据真正的应用层内容再选择服务器,只能先代理最终的服务器和客户端建立连接(三次握手)后,才能看到客户端发送的真正应用层内容的报文,然后再根据该报文中的特定字段,再加上负载均衡设备设置的服务器选择方式,决定最终选择的内部服务器。负载均衡设备在这种情况下,更类似于一个代理服务器。负载均衡和前端的客户端以及后端的服务器会分别建立TCP连接。所以从这个技术原理上来看,七层负载均衡明显的对负载均衡设备的要求更高,处理七层的能力也必然会低于四层模式的部署方式。
语法:

1、upstream name { ... }
    定义后端服务器组,会引入一个新的上下文;Context: http
    
    upstream httpdsrvs {
        server ...
        server...
        ...
    }
    
2、server address [parameters];
    在upstream上下文中server成员,以及相关的参数;Context:  upstream
    
    address的表示格式:
        unix:/PATH/TO/SOME_SOCK_FILE
        IP[:PORT]
        HOSTNAME[:PORT]
        
    parameters:
        weight=number
            权重,默认为1;
        max_fails=number
            失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用;
        fail_timeout=time
            设置将服务器标记为不可用状态的超时时长;
        max_conns
            当前的服务器的最大并发连接数;
        backup
            将服务器标记为“备用”,即所有服务器均不可用时此服务器才启用;
        down
            标记为“不可用”;
            
3、least_conn;
    最少连接调度算法,当server拥有不同的权重时其为wlc;
    
4、  ip_hash;
    源地址hash调度方法;类似lvs的sh算法,使用了就不能使用backup了
    
5、hash key [consistent];
    基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者的组合;
    
    作用:将请求分类,同一类请求将发往同一个upstream server;
    
    If the consistent parameter is specified the ketama consistent hashing method will be used instead.
        

    示例:
        hash $request_uri consistent;
        这是根据请求的资源进行会话粘性,在代理服务器上都有一张hash表记录一切
        带consistent指定一致性哈希自带虚拟节点,就算后端节点失效了也能重新调度
        hash $remote_addr;
        hash $cookie_name

6、keepalive connections;
    为每个worker进程保留的空闲的长连接数量;
    为服务器端保持长链接,不需要一个并发访问就打开一个套接字端口

7、least_time header | last_byte;
    最短平均响应时长和最少连接;
    header:response_header; 
    last_byte: full_response; 
    
    仅Nginx Plus有效;
    
8、 health_check [parameters];
    定义对后端主机的健康状态检测机制;只能用于location上下文;
    
    可用参数:
        interval=time:检测频率,默认为每隔5秒钟;
        fails=number:判断服务器状态转为失败需要检测的次数;
        passes=number:判断服务器状态转为成功需要检测的次数;
        uri=uri:判断其健康与否时使用的uri;
        match=name:基于指定的match来衡量检测结果的成败;
        port=number:使用独立的端口进行检测;
        
    仅Nginx Plus有效;
        
9、 match name { ... }
    Defines the named test set used to verify responses to health check requests.
    定义衡量某检测结果是否为成功的衡量机制;
    
    专用指令:
        status:期望的响应码;
            status CODE
            status ! CODE
            ...
        header:基于响应报文的首部进行判断
            header HEADER=VALUE
            header HEADER ~ VALUE 
            ...
        body:基于响应报文的内容进行判断
            body ~ "PATTERN"
            body !~ "PATTERN"
            
    仅Nginx Plus有效;

示例:
在node1代理服务器上定义一个httpdsrvs组,七层负载均衡,基于uri的hash以保存会话粘性

    upstream httpdsrvs{
        hash $request_uri consistent;
        server node2.lvqing.com weight=2;
        server node3.lvqing.com weight=1  max_fails=3 fail_timeout=30s;
    }

    location / {
        proxy_cache pxycache;
        proxy_cache_key $proxy_host$request_uri;
        proxy_pass httpdsrvs;
    }

结果:
因为我们前面基于uri做了会话粘性,相同的请求都会发给后端的同一台服务器所以这里只有一台响应。当然如果node2挂掉会自动切换到node3

for i in {1..10};do curl http://node1.lvqing.com;done
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>

如果将上面的 hash $request_uri consistent;去掉会是一下的结果

for i in {1..10};do curl http://node1.lvqing.com;done
<h1>node2 Server</h1>
<h1>node3 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node3 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node3 Server</h1>
<h1>node3 Server</h1>

这是基于7层代理的,效率相对四层来说较低,但我们可以对请求做的操作也更多
13、ngx_stream_core_module
模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器,工作在四层的代理。
语法:
负载均衡设备在接收到第一个来自客户端的SYN 请求时,即通过上述方式选择一个最佳的服务器,并对报文中目标IP地址进行修改(改为后端服务器IP),直接转发给该服务器。TCP的连接建立,即三次握手是客户端和服务器直接建立的,负载均衡设备只是起到一个类似路由器的转发动作。在某些部署情况下,为保证服务器回包可以正确返回给负载均衡设备,在转发报文的同时可能还会对报文原来的源地址进行修改。

1、stream { ... }
定义stream相关的服务;Context:main

2、listen
listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] 
[reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
监听的端口;
    默认为tcp协议;
    udp: 监听udp协议的端口;

14、ngx_stream_proxy_module
在stream里使用的proxy语句,与stream配套使用
语法:

(1) proxy_pass address;
    Sets the address of a proxied server. The address can be specified as a domain name or IP address, and a port or as a UNIX-domain socket path.
    
(2) proxy_timeout timeout;
    Sets the timeout between two successive read or write operations on client or proxied server connections. If no data is transmitted within this time, the connection is closed.
    默认为10m; 
    
(3) proxy_connect_timeout time;
    Defines a timeout for establishing a connection with a proxied server.  
    设置nginx与被代理的服务器尝试建立连接的超时时长;默认为60s;

示例:

stream {
    upstream nginxsrvs {
        server 192.168.31.201:80; 
        server 192.168.31.203:80; 
        least_conn;
    }

    server {
        listen 192.168.31.200:8080;
        proxy_pass nginxsrvs;
        proxy_timeout 60s;
        proxy_connect_timeout 10s;
    }
}   

结果:

 for i in {1..10};do curl http://192.168.31.200:8080;done
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node3 Server</h1>
<h1>node3 Server</h1>
<h1>node2 Server</h1>
<h1>node2 Server</h1>
<h1>node3 Server</h1>
<h1>node2 Server</h1>
<h1>node3 Server</h1>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值