美文网首页
nginx之ngx_http_upstream_module模块

nginx之ngx_http_upstream_module模块

作者: 任总 | 来源:发表于2018-07-24 23:55 被阅读15次

一、ngx_http_upstream_module调度模块

  • 把后端服务器组成服务器组,然后调度,模块用于定义可由proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass 指令引用的服务器组

1、 upstream name { ... }

  • 定义后端服务器组;引入一个新的上下文;只能用于http{}上下文中;

  • 默认的调度方法是wrr;

2、 server address [parameters];

  • 定义服务器地址和相关的参数;

(1)地址格式:
IP[:PORT]
HOSTNAME[:PORT]
unix:/PATH/TO/SOME_SOCK_FILE

(2) 参数:
  • weight=number 权重,默认为1;
  • max_fails=number 失败尝试的最大次数;
  • fail_timeout=time 设置服务器为不可用状态的超时时长;
  • backup 把服务器标记为“备用”状态;
  • down 手动标记其为不可用;
(3) least_conn;

最少连接调度算法;当server拥有不同的权重时为wlc;当所有后端主机的连接数相同时,则使用wrr进行调度;

(4) least_time header | last_byte;

最短平均响应时长和最少连接;
header:response_header;
last_byte: full_response;

仅Nginx Plus有效;

(5) ip_hash;

源地址hash算法;能够将来自同一个源IP地址的请求始终发往同一个upstream server;

(6) hash key [consistent];

基于指定的key的hash表实现请求调度,此处的key可以文本、变量或二者的组合;

consistent:参数,指定使用一致性hash算法;

示例:
hash $request_uri consistent
hash $remote_addr
hash $cookie_name
(7) keepalive connections;

可使用长连接的连接数量;

(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"

二、基于服务器组调度示例

  • 调度VS服务设置
[root@vs conf.d]# vim ilinux.conf#编辑

server{
       listen 80;  #监听80端口
        server_name www.linux.io;#服务名称

        location / {
                root /data/nginx/html;
                proxy_pass http://webservs;#代理到web组服务器
}
}

[root@vs conf.d]# vim ../nginx.conf

http {
.........
    upstream websrvs{           #设置代理组
                 ip_hash; #绑定来自同一个客户端ip请求始终分发到同一主机
                hash $request_uri consistent;#绑定同一uri请求始终分发到同一主机,一致性哈希环算法
                 server 192.168.1.11:80 weight=2;#加权轮询
                 server 192.168.1.12:80 fail_timeout=1 max_fails=3;#检测失败超时1秒,最大检测失败3次
             server 127.0.0.1:80 backup;#当后端服务器全部离线,启用备用服务器
               }

客户端测试:

轮询调度:
[root@mysql ~]# curl http://www.ilinux.io/index.html
<h1>Upstream Server1</h1>
[root@mysql ~]# curl http://www.ilinux.io/index.html
<h1>Upstream Server2</h1>
[root@mysql ~]# curl http://www.ilinux.io/index.html
<h1>Upstream Server1</h1>
[root@mysql ~]# curl http://www.ilinux.io/index.html
<h1>Upstream Server2</h1>
加权轮询调度:
[root@mysql ~]# for i in {1..10};do curl http://www.ilinux.io/index.html;done
<h1>Upstream Server1</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server2</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server2</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server1</h1>
<h1>Upstream Server2</h1>
<h1>Upstream Server1</h1>

相关文章

网友评论

      本文标题:nginx之ngx_http_upstream_module模块

      本文链接:https://www.haomeiwen.com/subject/uqzlmftx.html