nginx负载均衡---stream

博客介绍了TCP/UDP相关内容,如UDP流量需包含udp参数。还阐述了多种负载均衡方法,包括轮询、最少连接、哈希、随机等,以及各方法的特点和适用场景。此外,提到了健康检查的参数fail_timeout和max_fails及其默认值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tcp/udp

  • For UDP traffic, also include the udp parameter. As TCP is the default protocol for the stream context, there is no tcp parameter to the listen directive
    example:

      stream {
      	server {
      		listen 12345;
      		# ...
      	}
    
      	server {
      		listen 53 udp;
      		# ...
      	}
      	# ...
      }
    

load balance

  1. Round Robin(default)

  2. Least Connections NGINX selects the server with the smaller number of current active connections

  3. Hash NGINX selects the server based on a user‑defined key, for example, the source IP address ($remote_addr):

     upstream stream_backend {
     	hash $remote_addr;
     	server backend1.example.com:12345;
     	server backend2.example.com:12345;
     	server backend3.example.com:12346;
     }
    

    The Hash load‑balancing method is also used to configure session persistence. As the hash function is based on client IP address, connections from a given client are always passed to the same server unless the server is down or otherwise unavailable. Specify an optional consistent parameter to apply the ketama consistent hashing method:

     hash $remote_addr consistent;
    
  4. Random – Each connection will be passed to a randomly selected server. If the two parameter is specified, first, NGINX randomly selects two servers taking into account server weights, and then chooses one of these servers using the specified method:

    least_conn – The least number of active connections

    The Random load balancing method should be used for distributed environments where multiple load balancers are passing requests to the same set of backends. For environments where the load balancer has a full view of all requests, use other load balancing methods, such as round robin, least connections and least time.

  5. Optionally, for each upstream server specify server‑specific parameters including maximum number of connections, server weight, and so on:

     upstream stream_backend {
     	hash   $remote_addr consistent;
     	server backend1.example.com:12345 weight=5;
     	server backend2.example.com:12345;
     	server backend3.example.com:12346 max_conns=3;
     }
     upstream dns_servers {
     	least_conn;
     	server 192.168.136.130:53;
     	server 192.168.136.131:53;
     	# ...
     }
    

Health Checks

  1. fail_timeout
  2. max_fails

The default values are 10 seconds and 1 attempt

upstream stream_backend {
	server backend1.example.com:12345 weight=5;
	server backend2.example.com:12345 max_fails=2 fail_timeout=30s;
	server backend3.example.com:12346 max_conns=3;
}

参考文献:https://docs.nginx.com/nginx/admin-guide/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值