'Nginx: limit_conn vs upstream max_conns (in location context)

Environment: Nginx 1.14.0 (see dockerfile for more details).

To limit the number of concurrent connections for a specific location
in a server, one can use two methods - limit_conn (third example for all ips)
and upstream max_conns.
Is there a difference in the way the two methods works?
Can someone explain or refer to explanation?

example of limiting using upstream max_conns:

http {
   upstream foo{
     zone upstream_foo 32m;
     server some-ip:8080 max_conns=100;
   }

   server {
    listen 80;
    server_name localhost;

    location /some_path {
       proxy_pass http://foo/some_path;
       return 429;
    }
   }  
}

limiting using limit_conn:

http {

   limit_conn_zone $server_name zone=perserver:32m;

   server {
    listen 80;
    server_name localhost;

    location /some_path {
       proxy_pass http://some-ip:8080/some_path;
       limit_conn perserver 100;
       limit_conn_status 429;
    }
   }  
}


Solution 1:[1]

Also note that, if the max_conns limit has been reached, the request can be placed in a queue for further processing, provided that the queue (NGINX Plus) directive is also included to set the maximum number of requests that can be simultaneously in the queue:

upstream backend {
    server backend1.example.com max_conns=3;
    server backend2.example.com;
    queue 100 timeout=70;
}

If the queue is filled up with requests or the upstream server cannot be selected during the timeout specified by the optional timeout parameter, or the queue parameter is omitted, the client receives an error (502).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CrazyRabbit