<p>I am trying to rate limit number GRPC connections based on a token included in the Authorization header. I tried the following settings in the Nginx configmap and Ingress annotation but Nginx rate limiting is not working.</p>
<pre><code>---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-ingress-controller
  namespace: default
data:
  http-snippet: |
    limit_req_zone $http_authorization zone=zone-1:20m rate=10r/m;
    limit_req_zone $http_token zone=zone-2:20m rate=10r/m;

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/backend-protocol: GRPC
    nginx.ingress.kubernetes.io/configuration-snippet: |
      limit_req zone=zone-1;
      limit_req_log_level notice;
      limit_req_status 429;
</code></pre>
<p>I try to have Nginx Ingress Controller to rate limit the GRPC/HTTP2 stream connection based on the value in the $http_authorization variable. I have modified the Nginx log_format to log the $http_authorization value and observe that Nginx receives the value. The problem I am facing is that for some reason the rate limiting rule doesn't get triggered.</p>
<p>Is this the correct approach?</p>
<p>Any help and feedback would be much appreciated!</p>
<p>Thanks</p>

Bobby H
<p>Hello Bobby_H and welcome to Stack Overflow!</p>
<p>When using Nginx Ingress on Kubernetes you can set up your rate limits with <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#rate-limiting" rel="nofollow noreferrer">these annotations</a>:</p>
<blockquote>
<ul>
<li><p><code>nginx.ingress.kubernetes.io/limit-connections:</code> number of concurrent connections allowed from a single IP address. A 503 error
is returned when exceeding this limit.</p>
</li>
<li><p><code>nginx.ingress.kubernetes.io/limit-rps:</code> number of requests accepted from a given IP each second. The burst limit is set to this limit
multiplied by the burst multiplier, the default multiplier is 5. When
clients exceed this limit, limit-req-status-code default: 503 is
returned.</p>
</li>
<li><p><code>nginx.ingress.kubernetes.io/limit-rpm:</code> number of requests accepted from a given IP each minute. The burst limit is set to this limit
multiplied by the burst multiplier, the default multiplier is 5. When
clients exceed this limit, limit-req-status-code default: 503 is
returned.</p>
</li>
<li><p><code>nginx.ingress.kubernetes.io/limit-burst-multiplier:</code> multiplier of the limit rate for burst size. The default burst multiplier is 5, this
annotation override the default multiplier. When clients exceed this
limit, limit-req-status-code default: 503 is returned.</p>
</li>
<li><p><code>nginx.ingress.kubernetes.io/limit-rate-after:</code> initial number of kilobytes after which the further transmission of a response to a
given connection will be rate limited. This feature must be used with
proxy-buffering enabled.</p>
</li>
<li><p><code>nginx.ingress.kubernetes.io/limit-rate:</code> number of kilobytes per second allowed to send to a given connection. The zero value disables
rate limiting. This feature must be used with proxy-buffering enabled.</p>
</li>
<li><p><code>nginx.ingress.kubernetes.io/limit-whitelist:</code> client IP source ranges to be excluded from rate-limiting. The value is a comma
separated list of CIDRs.</p>
</li>
</ul>
</blockquote>
<p>Nginx implements the <a href="https://en.wikipedia.org/wiki/Leaky_bucket" rel="nofollow noreferrer">leaky bucket</a> algorithm, where incoming requests are buffered in a FIFO queue, and then consumed at a limited rate. The burst value defines the size of the queue, which allows an exceeding number of requests to be served beyond the base limit. When the queue becomes full, the following requests will be rejected with an error code returned.</p>
<p><a href="http://nginx.org/en/docs/http/ngx_http_limit_req_module.html" rel="nofollow noreferrer">Here</a> you will find all important parameters to configure your rate limiting.</p>
<p>The number of expected successful requests can be calculated like this:</p>
<pre><code>successful requests = (period * rate + burst) * nginx replica
</code></pre>
<p>so it is important to notice that the number of nginx replicas will also multiply the number of successful requests. Also, notice that Nginx ingress controller sets burst value at 5 times the limit. You can check those parameters at <code>nginx.conf</code> after setting up your desired annotations. For example:</p>
<pre><code>limit_req_zone $limit_cmRfaW5ncmVzcy1yZC1oZWxsby1sZWdhY3k zone=ingress-hello-world_rps:5m rate=5r/s;
limit_req zone=ingress-hello-world_rps burst=25 nodelay;

limit_req_zone $limit_cmRfaW5ncmVzcy1yZC1oZWxsby1sZWdhY3k zone=ingress-hello-world_rpm:5m rate=300r/m;
limit_req zone=ingress-hello-world_rpm burst=1500 nodelay;
</code></pre>
<p>There are two limitations that I would also like to underline:</p>
<ul>
<li><p>Requests are counted by client IP, which might not be accurate, or not fit your business needs such as rate-limiting by user identity.</p>
</li>
<li><p>Options like burst and delay are not configurable.</p>
</li>
</ul>
<p>I strongly recommend to go through the below sources also to have a more in-depth explanation regarding this topic:</p>
<ul>
<li><p><a href="https://www.freecodecamp.org/news/nginx-rate-limiting-in-a-nutshell-128fe9e0126c/" rel="nofollow noreferrer">NGINX rate-limiting in a nutshell</a></p>
</li>
<li><p><a href="https://www.nginx.com/blog/rate-limiting-nginx/" rel="nofollow noreferrer">Rate Limiting with NGINX and NGINX Plus</a></p>
</li>
</ul>
