Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Please provide the relevant nginx configuration. –  mgorven Jul 12 '12 at 2:48
    
Related: serverfault.com/questions/30705/… –  Michael Hampton Jul 12 '12 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 '12 at 3:23

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
that sounds great, do you happen to have a script? :) –  Alex Jul 12 '12 at 9:20

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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