Setting cache parameters via if directives

Jeff Mitchell jeff at
Fri Jan 28 08:08:25 MSK 2011


We have a particular application (gitweb) that performs a particular,
extraordinarily slow function when the home page is loaded. As the
number of repositories has increased, this has grown to take several
*minutes* per page view (yes, ugh).

To combat this, we tried setting up cache so that this only occurs once
an hour, but it's still causing too much of a problem...both because it
doesn't always last an hour (sometimes we see the delay happen again
sooner) and because when it does expire, it simply takes too long (and
if multiple clients connect at that same time making that request, it
gets even worse).

As a result, what I'd like to do is to have the cache expire after an
hour, but once every twenty or thirty minutes manually refresh the
cache. That way clients can use the existing cache while the new page is
being rebuilt, which will then save the new values into the cache and
reset the timer.

So my thought was that I could do something like the code below, but it
doesn't work since I can't put the directives I want into the if block,
and the proxy_pass in the if block (if I remove the cache values) honors
the cache values in the outside location block anyways:

    location = / {
        if ($remote_addr = "") {
            proxy_cache gitweb;
            proxy_cache_valid 200 20m;
            proxy_read_timeout 300;
            proxy_send_timeout 300;
        proxy_cache gitweb;
        proxy_cache_valid 200 60m;
        proxy_cache_use_stale off;
        proxy_read_timeout 60;
        proxy_send_timeout 60;

Any help much appreciated.


More information about the nginx mailing list