upstream_conf
ngx_http_api_module
ngx_http_dyups_module
lua-nginx-module
ngx.shared.DICT
nginx-upsync-module
nginx-ingress-controller
resolver
resolver 8.8.8.8 valid=10s; upstream backend { server backend.example.com resolve; }
valid
http { lua_shared_dict dynamic_upstreams 10m; upstream backend { server 0.0.0.0; # 占位符 balancer_by_lua_block { local balancer = require "ngx.balancer" local upstreams = ngx.shared.dynamic_upstreams local peers = upstreams:get("backend_peers") or "127.0.0.1:8080" -- 解析peers并设置当前请求的后端 balancer.set_current_peer(host, port) } } location /update { content_by_lua_block { -- 从请求参数或外部存储获取新节点列表 local new_peers = ngx.var.arg_peers ngx.shared.dynamic_upstreams:set("backend_peers", new_peers) ngx.say("Updated") } } }
健康检查:
max_fails
fail_timeout
health_check
nginx_upstream_check_module
负载均衡策略:
性能与缓存:
高可用:
安全风险:
Nginx开源版本身不支持动态upstream,但可通过第三方模块、服务发现或OpenResty扩展实现。选择方案时需权衡动态性、复杂性和维护成本。商业版Nginx Plus提供最完整的动态管理功能,适合企业级场景。