美文网首页
NGINX ngx_http_upstream模块说明

NGINX ngx_http_upstream模块说明

作者: 二手三流科学家 | 来源:发表于2019-03-09 18:25 被阅读0次

首先,NGINX负载均衡指定后端节点

upstream myWebs { #这个指令属于ngx_http_upstream_module模块
    server 172.16.1.7 max_fails=1 fail_timeout=10s;
    server 172.16.1.8 max_fails=1 fail_timeout=10s;
    #综合几种负载均衡的策略,这里选择least_time,下面给出说明
    #Specifies that a group should use a load balancing method 
    #where a request is passed to the server 
    #with the least average response time 
    #and least number of active connections, 
    #taking into account weights of servers. 
    #If there are several such servers, 
    #they are tried in turn using a weighted round-robin balancing method.

    #If the header($upstream_header_time) parameter is specified, 
    #time to receive the response header is used. 
    #If the last_byte($upstream_response_time) parameter is specified, 
    #time to receive the full response is used. 
    #If the inflight parameter is specified, 
    #incomplete requests are also taken into account.
    #但是,least_time只有Nginx Plus中才有,换成least_conn吧
    #least_time last_byte;
    #least_conn;
    #下面是keepalive的说明:
    #首先是nginx.conf中events中的
    #worker_connections:限制每个worker progress中的最大连接数
    #对于反向代理端来说,这个最大连接数不仅包括与client端的连接数,
    #也包括与upstream端的连接数
    #然后keepalive指定的connections是指反向代理端与upstream端的最大长连接数,
    #即:The connections parameter sets the maximum number of 
    #idle keepalive connections to upstream servers 
    #that are preserved in the cache of each worker process. 
    #It should be particularly noted 
    #that the keepalive directive does not limit 
    #the total number of connections to upstream servers 
    #that an nginx worker process can open.
   
   
    #如果worker_connections给成默认的1024,
    #假设客户端发送的都是长连接,
    #而且反向代理发送到upstream端的也都是长连接,
    #那keepalive指定的connection最大应为512。
    #反向代理端与upstream端的长连接数目如果超过了connections,
    #会有一个机制,即
    #When this number is exceeded, 
    #the least recently used connections are closed. 
    #然后client端与反向代理端没有这个机制,
    #假设反向代理端与upstream端的长连接数为512,
    #然后client端与反向代理端的连接数(包括长连接和短连接)超过了512
    #那么,client端就无法与反向代理端建立连接
    #所以,为了保证client端与反向代理端能够建立连接,
    #应将connections给成一个较小的值,即
    #The connections parameter should be set to a number small enough 
    #to let upstream servers process new incoming connections as well. 
    #给成官方文档中的例子
    keepalive 16;
}

location / {
    #proxy_pass指令属于ngx_http_proxy_module模块
    proxy_pass http://myWebs ;
    #由于ngx_http_proxy模块默认会将反向代理请求的connection头部设置成Close
    #因此这里也需要清除Connection头部
    #清除头部即不发送该头部,在HTTP 1.1中默认为长连接,
    #这样,反向代理端与upstream端建立的连接都是长连接
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header Host $http_host;
    
}

然后是官方文档中关于健康检查的说明:

Reverse proxy implementation in nginx includes in-band (or passive) server health checks. If the response from a particular server fails with an error, nginx will mark this server as failed, and will try to avoid selecting this server for subsequent inbound requests for a while.
The max_fails directive sets the number of consecutive unsuccessful attempts to communicate with the server that should happen during fail_timeout. By default, max_fails is set to 1. When it is set to 0, health checks are disabled for this server. The fail_timeout parameter also defines how long the server will be marked as failed. After fail_timeout interval following the server failure, nginx will start to gracefully probe the server with the live client’s requests. If the probes have been successful, the server is marked as a live one.

相关文章

网友评论

      本文标题:NGINX ngx_http_upstream模块说明

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