Hi,
I'm looking to use the max_conns parameter of the server directive in the nginx upsteam module to prevent too many connections overloading a backend (http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server).
We also use the ngx.balancer module (https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/balancer.md) to dynamically set the IP of the upstream via set_current_peer()
What I've noticed is that max_conns doesn't seem to have any impact when used in conjunction with set_current_peer(), yet works fine with a normally defined upstream.
The following config demonstrates:
daemon off;
error_log stderr warn;
worker_processes 1;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
# incoming requests land here
server {
listen 80;
location / {
return 200 'not the upstream\n';
}
location /static_upstream {
proxy_pass http://static_upstream;
}
location /lua_upstream {
proxy_pass http://lua_upstream;
}
}
# upstream configs
upstream static_upstream {
server 127.0.0.1:8888 max_conns=1; # the max_conns directive limits connections to the upstream
keepalive 1024;
}
upstream lua_upstream {
server 0.0.0.1 max_conns=1; # this max_conns directive doesn't have any impact
balancer_by_lua_block {
local balancer = require 'ngx.balancer'
balancer.set_current_peer('127.0.0.1','8888')
}
keepalive 1024;
}
# server to simulate an upstream
server {
listen 8888;
location / {
content_by_lua_block {
ngx.sleep(5)
ngx.say('This is the upstream')
}
}
}
}
When making requests to
localhost/static_upstream, I get a 502 and
[error] 498#498: *65 no live upstreams while connecting to upstream on the second and subsequent requests for 5 seconds which is what I expect with max_conns set to 1
Yet when hitting localhost/lua_upstream I am able to make multiple simultaneous requests and max_conns has no effect.
I take it the server directive is completely overridden by the set_current_peer() function? Is there any way to make max_conns work?
Thanks,
Rob