不信网恋 发表于 2018-11-8 11:17:31

nginx的upstream模块

  1、upstream 模块介绍:
  Nginx 的负载均衡功能依赖于ngx_http_upstream_modulemo模块,所支持的方式有
  proxy_pass,fastcgi_pass,memcached_pass.
  官方地址:http://nginx.org/en/docs/http/ngx_http_upstream_module.html
  upstream 模块语法:
  范例(1):
  upstream www_real_servers {
  #ip_hash;
  server 10.0.0.22:80weight=15;
  server 10.0.0.23:80weight=15;
  }
  解释:upstream是固定的;server是固定的后面可以接域名(门户会用)或ip,如果不加端口,默认是
  80,weight是权重,值越大被分配的几率越高。
  server backend2.example.com:8080 ;#域名加端口,转发到后端的端口。
  server unix:/tmp/backend3       #指定socket文件
  提示:Server如果接域名,需要内网有DNS服务器,或者在负载均衡器的hosts文件做域名解析。
  server backup1.example.com:8080 backup;
  #备份服务器,等上面指定的服务都不可以访问的时候会启用,back的用户和haproxy的用法相同。
  两台keepalive+http做高可用,那么这里使用nginx的backup参数就可以实现。
  2、upstream模块的相关说明:
  官方文档:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
  a、upstream模式应防御nginx.cnf配置的http{}标签内。
  b、upstream模式默认算法是wrr(权重轮询weighted round-robin)
  c、upstream模块内部部分参数说明:
  server 10.0.0.22:80
  负载均衡后面RS配置,可以是ip或域名,端口默认80.高并发场景ip要换成域名,通过dns做负载均衡。
  weight
  权重,默认是1
  注意:当负载均衡调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不可以是weight和backup
  max_fails=2
  最大尝试失败的次数,默认为1,0表示禁止失败尝试,京东1次,蓝汛10次,根据业务配置;
  如果max_fails是5,它就检测5次,如果5次都是502,他就会根据fails_timeout的值等待后再次检查。
  这个参数配合proxy_next_upstream\fastcgi_next_upstream\memcached_pass 这三个参数使用,当nginx接受后端服务器返回这三个参数
  定义的状态码的时候,会将这个请求转发给正常工作的后端服务器,例如404,502,503,Max_fails默认值为1(提升用户体验)
  backup
  热备配置(RS节点的高可用) ,当前面激活的RS 都失败后会自动启动热备RS
  fail_timeout=20s
  失败超时时间,默认是10s,京东3秒,蓝汛10秒,根据业务配置。常规2-3秒
  down
  标志服务器永远不可用,这个参数一直配合ip_hash使用。
  3、特别说明:
  对于nginx代理cache服务时,可能需要使用hash算法,此时,如果宕机时,可通过设置down参数确保
  客户端使用安装当前的hash算法访问,很重要滴!!
  官网内容如下:
  If one of the servers needs to be temporarily removed, it should
be marked with thedown parameter in
order to preserve the       current hashing of client IPaddresses.
  Example:
upstream backend {  ip_hash;
  server backend1.example.com;
  server backend2.example.com;
  server backend3.example.com down;
  server backend4.example.com;
  }
  Until versions 1.3.1 and 1.2.2, it was not possible to specify a weight for
servers using the ip_hash load balancing method.
  4、upstream 算法:
  rr 轮询:
  按客户端请求顺序把客户端的请求逐一分配到不同的服务器后端;
  weight权重:
  在轮询算法的基础上加上权重(默认是rr+weight),权重轮询和访问成正比。
  ip_hash:
  每个请求按访问ip的hash结果分配,当新的请求到达时,先将其客户端ip通过哈希算法哈希出一
  个值,在随后请求客户端,ip的哈希值只要相同,就会被分配到同一台服务器(lvs负载均衡-P参数。
  keepalive里配置的persistence_timeout 50),该调度算法有效解决动态网页session共享问题,容
  易造成请求分配不均。
  注意:当调度算法为ip_hash时,后端服务器在负载均衡中的状态不能是weight、backup。
  fair (第三方 no)
  按照后端服务器相应时间分配请求,相应时间短优先分配。
  范例:
  upstream www_real_servers {
  server 10.0.0.22:80weight=15;
  server 10.0.0.23:80weight=15;
  fair;
  }
  #使用时下载nginx的upstream_fair模块
  url_hash(第三方 no)
  按访问url的hash结果分配请求,让每个url定向到同一个后端服务器(后端服务器为缓存服务器时效果著,cdn)。
  在upstream中加入hash语句,server语句不可以加入weight等其它参数
  范例:
  upstream www_real_servers {
  server 10.0.0.22:80weight=15;
  server 10.0.0.23:80weight=15;
  hash $request_uri;
  }
  least_conn
  最少连接数,那个机器连接数少就分发
  一致性hash
  http://tengine.taobao.org/document_cn/http_upstream_consistent_hash_cn.html
  #缓存负载均衡最好的算法
http {  upstream test {
  consistent_hash $request_uri;

  server 127.0.0.1:9001>
  server 127.0.0.1:9002>
  server 127.0.0.1:9003>  }


页: [1]
查看完整版本: nginx的upstream模块