title: nginx set remaining count for limit_req in X-RateLimit-Remaining header
body: <p>I'm actually a little surprised that I couldn't find anything after a couple hours of googling, but the problem is as follows:</p>
  
  <p>I want nginx to serve as my throttle for my API.</p>
  
  <p>My config file contains a well-cited example of <code>limit_req_zone</code>:</p>
  
  <p><code>limit_req_zone $binary_remote_addr zone=limit:2m rate=10r/m;</code></p>
  
  <p>along with my <code>location</code> directive containing the expected <code>limit_req zone=limit nodelay;</code></p>
  
  <p>I would love to have nginx attach headers to the response message for both the <code>X-RateLimit-Remaining</code> and <code>X-RateLimit-Reset</code> attributes.  Basically have nginx use the active count of the <code>rate=10r/m</code> to populate <code>X-RateLimit-Remaining</code> and timeframe of the same <code>rate=10r/m</code> value to populate <code>X-RateLimit-Reset</code> with how many seconds are left before a refresh.</p>
  
  <pre><code>http {
      limit_req_zone $binary_remote_addr zone=login:10m rate=2r/s;
      limit_req_status 429;
      limit_conn_status 429;
  
      server {
          listen       80;
          server_name  [removed];
  
          location / {
              limit_req zone=limit nodelay;
  
              proxy_pass http://reverse-proxy-example;
              add_header  X-RateLimit-Remaining [nginx variable?];
              add_header  X-RateLimit-Reset [nginx variable?]
          }
  }
  </code></pre>
  
  <p>Thoughts?  Possible?  Would love to avoid hitting the application to get these numbers.</p>