title: Nginx check if Cloudflare forward or direct IP and limit accordingly
body: <p>I am aware of the headers <code>CF-Connecting-IP</code>, <code>$binary_remote_addr</code>, <code>http_x_forwarded_for</code></p>
  <p>I want to make a setting:</p>
  <pre><code>limit_req_zone $http_x_forwarded_for zone=k_request_limit_per_ip:10m rate=400r/s;
  limit_conn_zone $http_x_forwarded_for zone=k_connection_limit_per_ip:10m;
  </code></pre>
  <hr />
  <p>But Cloudflare isn't the only place that this machine is going to be accessed, so I want to limit direct access too.
  Is there a way to write something like:</p>
  <pre><code>if(header == `X-Forwarded-For`) {
    limit_req_zone $http_x_forwarded_for zone=k_request_limit_per_ip:10m rate=400r/s;
  } else {
    limit_req_zone $binary_remote_addr zone=k_request_limit_per_ip:10m rate=400r/s;
  }
  </code></pre>
  <p>Or would something like this work:</p>
  <pre><code>  limit_req_zone $http_x_forwarded_for zone=http_zone:10m rate=400r/s;
    limit_req_zone $binary_remote_addr zone=binary_zone:10m rate=400r/s;
  </code></pre>
  <p>An alternative would be to fully allow all Cloudflare IP addresses. And limit the Non-Cloudflare  IP addresses.</p>
  <p>Good source:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/61620292/nginx-rate-limiting-doesnt-work-when-using-cloudflare-i-can-bring-down-my-site">NGINX rate limiting doesn&#39;t work when using Cloudflare. I can bring down my site with a simple `ab` command</a></li>
  </ul>