Re: “max conns” in upstream is not working what I've expected;

Vladimir Homutov vl at nginx.com
Wed Aug 22 09:57:21 UTC 2018


On Tue, Aug 21, 2018 at 10:58:15PM -0400, jinsam.kim wrote:
> Hello Super Heroes.
>
> I want to limit connections in a service. So I used max_conns directive in
> upstream.
>
> But it allows twice connections as many as I've set.
>
> So, I suspected myself. Maybe… Am I used the direction in wrong way.
>
> And I found some descriptions about “max_conns” exception rules.
>
> http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server
>
> ----
> If idle keepalive connections, multiple workers, and the shared memory are
> enabled, the total number of active and idle connections to the proxied
> server may exceed the max_conns value.
> ----
>
> And I’ve tried change configuration of “keepalive”, “worker numbers” and
> “shared memory”. But it still the same. It allowed twice connections as many
> as I expected.
>
>
> My config file is blow.
>
> #
> # max_conns Test Server
> #
>
> proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=my_cache:10m
> max_size=10g inactive=60s use_temp_path=off;
>
> proxy_cache_key "$scheme$request_method$host$request_uri";
>
> upstream t10 {
>     zone t1_z 1M;
>     server localhost:8080  max_conns=5 max_fails=0 fail_timeout=1s;
> }
>
> server {
>     listen 8000 ;
>     location /test/1 {
>         proxy_pass http://t10;  # it takes 1 seconds
>     }
> }
>
> And I run a shell script blow.
>
> for v in {1..40}
> do
> 	curl 'localhost:8000/test/1' -i -l >> 30_2.log &
> 	sleep 0.01
> done
>
> ----
>
> The url ‘localhost:8080/test/1’ takes 1 seconds.
>
> I expected 5 succeed and 35 fail. But it always 10 succeed, 30 failed.
>

Note that you are using 'localhost' in your upstream definition, not IP
address. Most probably it resolves into 2 addresses (127.0.0.1 and [::1]
I assume), so what you see is expected - each of peers processes 5
connections, giving 10 summary, and 30 are rejected, and 'no live
upstreams' are shown in error.log;

the configuration with hostname in your case is equal to:

upstream t10 {
    zone t1_z 1M;
    server 127.0.0.1:8080  max_conns=5 max_fails=0 fail_timeout=1s;
    server [::1]:8080  max_conns=5 max_fails=0 fail_timeout=1s;
}



More information about the nginx mailing list