<div dir="ltr"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><span style="font-size:12.8px">Hi,</span></div><div><span style="font-size:12.8px"><br></span></div><div dir="ltr"><span style="font-size:12.8px">I use Nginx as reverse proxy, and I would like to rate limit the requests to origin server, but only limit the requests with cache status EXPIRED. I just tested with a map "cache_key", and the rate limiting doesn't work, the</span><span style="font-size:x-small"> $cache_key</span><span style="font-size:12.8px"> was logged as empty string. But changing $upstream_cache_status to non-upstream variables like $remote_addr and adding an IP match value will make the rate limiting work. </span><span style="font-size:12.8px">The zone I defined like so:</span><div style="font-size:12.8px"><div><div><font size="1"> limit_req_zone $cache_key zone=cache_host:1m rate=1r/m;</font></div><div><font size="1"> map $upstream_cache_status $cache_key {</font></div><div><font size="1"> EXPIRED $host;</font></div><div><font size="1"> default "";</font></div><div><font size="1"> }</font></div></div><div>I enabled cache setting in nginx.conf, and one of my server chunk uses the rate limit zone like below:</div><div> <font size="1"> limit_req zone=cache_host busrt=1;</font></div><div><br></div></div><div style="font-size:12.8px">Is this because $upstream_cache_status value is set after the request is sent to origin server and got the response, while <span style="font-size:x-small">$cache_key</span> is used in rate limit zone which checked before the request was sent to origin server? <span style="font-size:12.8px">If so, is there a recommended way to implement rate limiting only for requests with specific cache status?</span></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Thanks! </div><div style="font-size:12.8px">Linna</div></div></div></div></div>
</div>