bypassing and replacing cache if origin changed - or purging cache when origin changed?

Francis Daly francis at daoine.org
Wed Feb 6 00:32:20 UTC 2019


On Fri, Jan 25, 2019 at 03:28:42PM -0500, nenaB wrote:

Hi there,

None of the suggestions below are fully tested by me, so they are not
drop-in answers to your questions.

> I have nginx configured to cache certain requests mapped by
> tenant/host/uri/args - these files wouldn't change often, but if they do, i
> want nginx to fetch them new.
> 
> I don't have NginxPlus - and I'm running Nginx docker.  Some things that I
> have investigated, but not sure how I could configure to make it work is: 
> the proxy_cache_bypass and the expires settings.

"Normal" caching involves nginx accepting what the upstream says the
expiry time of the response is.

If you want nginx to check upstream for new content before that expiry
time, you either need to purge nginx's cache for that request, or to
bypass the cache for the request.

I think that there is not an explicit "purge" facility in the stock
nginx. But there is the proxy_cache_bypass directive.

You can perhaps try a third-party module for purging elements from the
cache. Or set up your system such that big or expensive content never
expires, and instead have small or lightweight content that links to
the "current" big content, and the link is updated when new big content
is available.

Or you can just use proxy_cache_bypass.

If you know by some external means that the content that claimed to
be valid for the next month, is now in fact stale; then you can make
a request of nginx for the content while including some "please bypass
the cache" flags in the request, and nginx should make the request of
upstream, and store the response such that the next "normal" request
will get the new response.

> What exactly do I need to configure if
> I want to test whether for a given cache-key on the upstream server, is
> there new content? (I can implement an api that could test whether new
> content is available.)

I am assuming that you know when content has been changed, and you want
to tell nginx to refresh its cache even though nginx was previously told
that the content would not have changed yet.

In that case: ignore the cache key. Just make the normal request; but
make it from a specific source IP address and/or include a specific header
in the request, that causes your proxy_cache_bypass variable to be non-zero.


If you do not know whether the upstream content has changed or not,
perhaps you could set a short "expiry" time within nginx, and use
proxy_cache_revalidate to "refresh" the validity without re-fetching
the content?

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list