Nginx开源版1.31.0以后开放负载均衡算法least_time
一.新增基于响应时间维度的动态负载均衡策略
# 升级开源 Nginx 到 1.31.0+
Nginx 官方文档里 least_time 指令下面也标注:Prior to version 1.31.0, this directive was available only as part of commercial subscription,意思是 1.31.0 之前它只属于商业版功能。
来源:nginx.org CHANGES (https://nginx.org/en/CHANGES)
ngx_http_upstream_module 文档 (https://nginx.org/en/docs/http/ngx_http_upstream_module.html)
官方文档见 supported platforms(https://nginx.org/en/linux_packages.html)
二.centos7源码安装使用nginx
1. 安装编译依赖
yum install -y gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel wget tar
如果还要 image_filter、xslt、perl 等模块,需要再装:
yum install -y gd gd-devel libxml2 libxml2-devel libxslt libxslt-devel perl-devel
2. 下载 nginx 1.31.x
当前 mainline 是 1.31.1,least_time 从 1.31.0 开始支持。下载页见官方 (https://nginx.org/en/download.html)
wget https://nginx.org/download/nginx-1.31.1.tar.gz tar xf nginx-1.31.1.tar.gz cd nginx-1.31.1
3. 编译安装
先用一个相对稳妥的配置:
./configure \ --prefix=/usr/share/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib64/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/run/nginx.pid \ --lock-path=/run/lock/subsys/nginx \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-threads \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-http_gunzip_module \ --with-http_slice_module \ --with-http_secure_link_module \ --with-http_sub_module \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-stream \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-pcre \ --with-pcre-jit
然后:
make make install
4. 检查版本和配置
nginx -v
nginx -t
应该看到类似:
nginx version: nginx/1.31.1
5. 启动或重启
如果原来就是 systemd 管理:
systemctl restart nginx
systemctl status nginx
6. 测试 least_time
配置示例:
upstream backend {
least_time header;
server 192.168.206.11:8080;
server 192.168.206.12:8080;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
检查:
nginx -t
systemctl reload nginx
配置2:
# 探测频率太高,可以加 zone 让统计更准确
upstream backend {
zone backend 64k;
least_time header;
#least_time last_byte;
server 192.168.206.11:8080;
server 192.168.206.12:8080;
}
server {
listen 80;
location /nginx_status {
stub_status;
}
location / {
proxy_pass http://backend;
}
}
三.其他第三方模块编辑进去
1.vts 监控模块
需要下载 /root/nginx-module-vts
git clone https://github.com/vozlt/nginx-module-vts.git
然后在 ./configure 末尾加:
--add-module=/path/to/nginx-module-vts
2.主动健康检查模块nginx_upstream_check_module(不能和上面模块并存)
这个模块会让 nginx 在后台定期主动探测每个后端的响应时间,请求过来时直接选最快的。
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
然后在 ./configure 末尾加:
--add-module=/path/to/nginx_upstream_check_module
配置示例:
upstream backend {
least_time header;
server 192.168.206.11:8080;
server 192.168.206.12:8080;
check interval=3000 rise=2 fall=3 timeout=1000 type=http;
check_http_send "GET / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}

浙公网安备 33010602011771号