Purge the Nginx cache

Francis Daly francis at daoine.org
Wed May 4 13:06:12 UTC 2022

On Tue, May 03, 2022 at 09:47:03PM +0530, Kaushal Shriyan wrote:

Hi there,

> Thanks Francis for the detailed explanation and much appreciated !!!. We
> typically clear the nginx cache manually using the below command.
> #rm -rf /var/www/nginx/cache/content/*
> #rm -rf /var/www/nginx/cache/files/*

That will remove from the filesystem anything that was in those
directories; that will have the effect of nginx not having a valid
response in the cache for an incoming request, so nginx will make the
request of upstream and handle the response.

> The bigger problem is that this way of purging the Nginx cache deletes
> everything that was cached by this Nginx reverse proxy server and adds
> overhead to the infrastructure impacting the performance resulting with a
> high number of requests per time unit.

Yes - that "purge" is "removal of everything from disk".

> Directive proxy_cache_bypass is a recommended approach to refresh the cache
> resource after successful retrieval of the contents from the upstream
> backend server.

Yes - "proxy_cache_bypass" will tell nginx to skip the "looks in the
cache" step of the processing, and just do all of the other steps.

It won't "purge" the cache at all; it will add-or-replace the cached
response for this one request. If that is what you want, then it is the
correct directive to use.

> I have deleted the cache by following the method
> #rm -rf /var/www/nginx/cache/content/*
> #rm -rf /var/www/nginx/cache/files/*
> #curl -I https://testnginxproxycachepurge.testintcraft.com -H 'Cache-Purge:
> true'
> HTTP/1.1 200 OK

> *X-Cache-Status: MISS*

> #curl -I https://testnginxproxycachepurge.testintcraft.com -H 'Cache-Purge:
> true'
> HTTP/1.1 200 OK

> *X-Cache-Status: HIT*#

> I am seeing it as HIT instead of BYPASS in context to Nginx cache. I am
> attaching the config file for your reference. Please comment and I look
> forward to hearing from you. Thanks in Advance.

You originally had the config that never set $bypass.

I suggested that you set $bypass by adding a "map" to sometimes set
$bypass to 1.

You added the "map", which was good; and then you also added code to
always set $bypass to 0, and then sometimes set it to something else.

So the second piece of code you added, undid what the first piece you
added did.

If you are going to "set $bypass 0;", then you may as well remove the
"map" part again.

What does the nginx access log say about this request? Most importantly:
what is the IP address of the client, as far as nginx is concerned? If
it does not match

        $remote_addr ~ "^($"

then your $bypass will remain as 0, and your cache will never be bypassed.

If you want to allow one set of IP addresses to invite nginx to bypass
the cache, put that IP address pattern in the "if $remote_addr" part.

If you want to allow any IP address to invite nginx to bypass the cache,
set $bypass for any IP address.

Good luck with it,

Francis Daly        francis at daoine.org

More information about the nginx mailing list