1

We have an Apache behind Nginx. Nginx is used to load balance between webservers and to cache static content. The only problem is, every time the web application is updated we have to completely clear Nginx cache (static file cache is set to quite a high value, some static files change often others not).

Is there a clever way to let Nginx periodically check that the file has been modified (note: Apache returns "Date" in response headers) since the time file had been cached by Nginx.

Alex
  • 1,768
  • 4
  • 30
  • 51

2 Answers2

4

You'll have to write a script that periodically crawls your internal content that you want cached, and have the script crawl each URL that you update.

In the script, set an HTTP header (the actual header doesn't matter) and then use proxy_cache_bypass to force nginx to retrieve it from upstream.

Example: Your cache priming script sets the HTTP header X-Really-Get-It: true. In nginx.conf you will set:

proxy_cache_bypass $http_x_really_get_it;

nginx will fetch the file from upstream instead of from cache, and then cache the result.

If you are willing and able to use third party nginx modules, you can also use the cache_purge module. It seems rather poorly documented, though.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
1

Just serve your content with the right caching information - this will solve the problem on your proxy and any ohers between you and the client, e.g.

cache-control: max-age=86400; s-maxage=3600

instructs browsers to cache for 1 day, but proxies should cache for 1 hour.

But a better solution is to use different URLs for updated content.

symcbean
  • 19,931
  • 1
  • 29
  • 49
  • You are talking about browser caching - it has nothing to do with OPs question - server based caching – inemanja Feb 15 '18 at 22:10
  • 1
    @inemaja: no. The headers I've described here have **everything** to do with caching on a reverse proxy. Please rtfm. – symcbean Feb 15 '18 at 22:36
  • OP is talking about problem with cached content after updating files. For example: dev team (unexpectedly) needed to update example.js file that is being cached - and he don't want to flush all cached files... In my company we are changing urls - as you suggested (actually we are adding/changing some kind of timestamp on urls we are caching: example.js?v=1212 - but principle is similar)... And think server caching is losing purpose when set to value as low as 1 hour. – inemanja Feb 15 '18 at 23:20