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.

3
  • Please provide the relevant nginx configuration.
    – mgorven
    Jul 12, 2012 at 2:48
  • Related: serverfault.com/questions/30705/… Jul 12, 2012 at 3:11
  • 1
    You really should use a webserver as a webserver, and a caching proxy as a caching proxy. "Right tool for the job" and all that.
    – womble
    Jul 12, 2012 at 3:23

2 Answers 2

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.

1
  • 1
    that sounds great, do you happen to have a script? :)
    – Alex
    Jul 12, 2012 at 9:20
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.

3
  • You are talking about browser caching - it has nothing to do with OPs question - server based caching
    – inemanja
    Feb 15, 2018 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, 2018 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, 2018 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.