Nginx后端限连模块详解-nginx-limit-upstream
1、后端限连模块(nginx-limit-upstream)介绍
1.1、官方README
When you use nginx as a proxy, there is a problem you may have to face:a upstream server is able to achieve high QPS within a relatively low resourcestress. For example, mysql works well at the speed of 8000-9000 QPS within 60 parallelconnections, but only 2000 QPS when the number of connections increases to 500.I do not find any module that will solve the problem because they all face client,not the upstream. I tried the limit_req, but failed. when I write down “limit_req rate=8000/s”,I find there are over 2000 active connections between nginx and mysql,and when I write down “limit_req rate=60/s”, Jesus, it is really 60 QPS for my nginx.So, I have a try, and the limit_upstream module comes. This module will limit thenumber of connections to a upstream server identified by its ip address. With it, Icould control the upstream connections. When it reaches the set threshold, new requestswill suspend and wait until a active upstream request finishes and wake them up,otherwise, they will die when they get timeout. Of course, I can also specify the lengthof waiting queue, and the waiting timeout, as limit_req do.In this module, each worker suspend its requests when upstreams’ counters reach thethreshold, and resume them or cancel them all by itself. The only things they shareare the global upstreams’ counters. A special case for a request be allowed to go toupstream even when the threshold is reached is that, no request related to that upstreamis being processed at that time for the worker. as a result, the maximum number ofestablished connections to a upstream server is (‘limit’ + ‘worker_processes’).
- 大致原意:某些短连接应用类似MYSQL在并发连接60时,可以达到8000-9000 QPS性能,反而在连接达到500时,仅有2000 QPS的性能,此模块便是解决此类场景,有效的控制后端连接并发数,防止后端应用出现阻塞、卡死等现象,始终保持后端服务器的负载处于健康状态(除极端情况外)。
1.2、模块官方下载地址
目前已支持Nginx-1.4.4、1.8.1、1.10.1、1.12.1等版本
- GitHub地址:
- README:
2、模块Install
1 | patch -p2 < nginx.patch (nginx 1.0.X, 1.2.X) |
3、模块Example
1 | http { |
4、模块配置语法
1 | #作用:定于一个内存共享缓冲区 |
1 | #释义:zone=引用如上内存池; limit=要限制的连接数量;backlog=挤压连接数量控制;timeout=设置超过挤压连接数的超时时间,若超时反馈408报错; |
1 | #释义:设置limit_upstream日志级别,默认输出error.log |