Simple question about proxy cache

John Moore grails at jmsd.co.uk
Fri Mar 22 11:18:50 UTC 2013


On 18/03/13 11:21, John Moore wrote:
> On 17/03/13 23:08, Maxim Dounin wrote:
>> Hello!
>>
>> On Sun, Mar 17, 2013 at 08:08:39PM +0000, John Moore wrote:
>>
>>> I've used nginx as a reverse proxy server for a long while but I've not
>>> tried out the proxy cache until today, and I have to say I'm a little
>>> bit confused by what I'm seeing in the cache log, and I'm wondering
>>> whether I've set things up correctly. My requirements are actually
>>> pretty simple. I have a couple of locations which I want to proxy to
>>> another server and cache the results. Thus:
>>>
>>>              location /media/house_images/{
>>>                proxy_pass http://backend;
>>>                proxy_cache one;
>>>             }
>>>
>>>             location /media/boat_images/{
>>>                proxy_pass http://backend;
>>>                proxy_cache one;
>>>             }
>>>
>>>
>>> Apart from this, I don't want any cacheing of responses to be done. I am
>>> assuming that the default is NOT to cache unless a cache zone is
>>> specified (at the server or location level, presumably), so either
>>> omitting a proxy_cache or specifying 'proxy_cache off' should be
>>> sufficient to achieve this, should it not?
>> Yes, without proxy_cache (or with "proxy_cache off") configured in
>> a location cache won't be used.
>>
>>> Two things are puzzling me, though. Firstly, in the cache log, I'm
>>> seeing the URLs of all kinds of requests which SHOULD NOT be cached, and
>>> I'm wondering whether all requests are logged whether they're cached or
>>> not - I certainly hope this is the case and it's not actually cacheing
>>> these responses. I would definitely prefer to only see entries in the
>>> log for requests matching locations for which a cache has been
>>> specified. I presume this is possible?
>> You can configure logs for a specific location, see
>> http://nginx.org/r/access_log.
>>
>>> Secondly, the very requests which I would expect to be cached are all
>>> showing up in the log with the word 'MISS' in the $upstream_cache_status
>>> column.
>> This usually happens if your backend doesn't specify allowed cache
>> time (in this case, proxy_cache_valid should be used to set one,
>> see http://nginx.org/r/proxy_cache_valid) or if backend responses
>> doesn't allow cache to be used (either directly with
>> Cache-Control/Expires headers, or indirectly with Set-Cookie
>> header, see http://nginx.org/r/proxy_ignore_headers).
>>
> Excellent - thanks, Maxim! That's got me sorted now, it all seems to be
> working as planned.
>
>

Actually, there is one final tweak I'd like. There are a number of 
different locations which I'd like to use the proxy cache for. I cannot 
repeat for each location the block where the cache log is defined (it 
rightly complains about duplication). So I have to define it at a server 
level instead. If I do this, though, then EVERY request for that server 
ends up being logged, even if there is no cache in force for the request 
location (i.e., the location has either 'proxy_cache off' or no 
proxy_cache definition. Is there a way I can configure things so that 
the only requests which are logged are ones from locations where a proxy 
cache is in force?



More information about the nginx mailing list