# Invalidate just one page under a location directive

**URL:** <https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570>\
**Category:** NGINX\
**Created:** [February 8, 2012, 8:44am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570 "2012-02-08T08:44:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 8, 2012, 8:44am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/1 "2012-02-08T08:44:49Z")

</div>

Hi all,

A section of my virtual(say /industry) is cached with

proxy\_cache\_key $scheme$host$request\_uri;

key. This will cache all pages under this virtual. I do cache  
invalidation  
by firing a request with

proxy\_cache\_bypass

Now if I need to invalidate cache for a page under this virtual, how  
should  
I go about doing it?

Say /industry is the location directive

I need to invalidate just

/industry/category/cars

How do I do this?

-Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 8, 2012, 10:07am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/2 "2012-02-08T10:07:15Z")

</div>

On Wednesday 08 February 2012 11:44:04 Quintin P. wrote:

> 

The best way would be to create a location for this particular page:

location /industry/category/cars {  
…  
}

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#location)**
>
> This directive appeared in version 1.11.8.

also, you can utilize the map directive functionality, e.g:

map $uri $is\_cached\_uri {  
default 1;  
/industry/category/cars 0;  
}

proxy\_cache\_bypass $is\_cached\_uri;

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 8, 2012, 10:37am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/3 "2012-02-08T10:37:40Z")

</div>

Thanks would be difficult because my intention is to maintain per page  
cache. So we are talking about 1000+ pages and more being created  
dynamically.

-Quintin

2012/2/8 Валентин Бартенев [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 8, 2012, 11:43am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/4 "2012-02-08T11:43:28Z")

</div>

On Wednesday 08 February 2012 13:36:31 Quintin P. wrote:

> Thanks would be difficult because my intention is to maintain per page  
> cache. So we are talking about 1000+ pages and more being created  
> dynamically.

Probably, I misunderstood. My solution switches cache off completely for  
particular uri.

Maybe this module is useful for you:

> **[FRiCKLE Labs / nginx / ngx\_cache\_purge](http://labs.frickle.com/nginx_ngx_cache_purge/)**

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 9, 2012, 6:05am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/5 "2012-02-09T06:05:23Z")

</div>

08 февраля 2012, 13:37 от Quintin P. : \> Thanks would be difficult  
because my intention is to maintain per page \> cache. So we are talking  
about 1000+ pages and more being created \> dynamically. \> \> -Quintin \> \>  
2012/2/8 Валентин Бартенев \> \> \> On Wednesday 08 February 2012 11:44:04  
Quintin P. wrote: \> \> \> Hi all, \> \> \> \> \> \> A section of my virtual(say  
/industry) is cached with \> \> \> \> \> \> proxy\_cache\_key  
$scheme$host$request\_uri; \> \> \> \> \> \> key. This will cache all pages  
under this virtual. I do cache \> \> invalidation \> \> \> by firing a  
request with \> \> \> \> \> \> proxy\_cache\_bypass \> \> \> \> \> \> Now if I need to  
invalidate cache for a page under this virtual, how \> \> should \> \> \> I  
go about doing it? \> \> \> \> \> \> Say /industry is the location directive \>

> > > > > I need to invalidate just \> \> \> \> \> \> /industry/category/cars
> > > > > 
> > > > > > How do I do this? Do you want to do per-request cache  
> > > > > > invalidation based on client information (header values, cookies,  
> > > > > > originating IP etc.), so the cache is bypassed whenever a client sends a  
> > > > > > request that matches your cache invalidation criteria, or do you want to  
> > > > > > be able to invalidate a certain URI for all future clients and requests  
> > > > > > by sending some kind of “switch caching off for this URI” request? Max

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 9, 2012, 6:22am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/6 "2012-02-09T06:22:04Z")

</div>

Simples way to say this would be, invalidate proxy\_cache\_key but not all  
of  
the cache.

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 8, 2012, 12:09pm UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/7 "2012-02-08T12:09:34Z")

</div>

I was hoping for a solution from the vanilla builds

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 9, 2012, 10:02am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/8 "2012-02-09T10:02:36Z")

</div>

Can someone help? My small site on a tiny VM is being bombarded and the  
cache invalidation is clearing up everything. Which means warming up the  
caching takes a lot of expensive queries.

-Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 9, 2012, 10:18am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/9 "2012-02-09T10:18:09Z")

</div>

On 8 Fev 2012 15h59 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can someone help? My small site on a tiny VM is being bombarded and  
> the cache invalidation is clearing up everything. Which means  
> warming up the caching takes a lot of expensive queries.

What cache invalidation? What caching strategy are you using?  
Are working with a content based expiration logic or from a  
microcaching perspective?

You can inspect the cache using a small shell script:

> **[GitHub - perusio/nginx-cache-inspector: Script that allows to inspect the Nginx cache](https://github.com/perusio/nginx-cache-inspector)**
>
> Script that allows to inspect the Nginx cache

— appa

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 9, 2012, 7:01am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/10 "2012-02-09T07:01:37Z")

</div>

I don’t think I made that clear.

To put it simply, Invalidate cache for a particular key entry but not  
all  
of the cache

e.g for

location = /vehicles {

proxy\_cache\_key $request\_uri;

}

Hashtable for cache:

[

hittp://jj.com/vehicles/cars =\> hittp://jj.com/vehicles/cars

hittp://jj.com/vehicles/bus =\> hittp://jj.com/vehicles/bus

hittp://jj.com/vehicles/bike=\> hittp://jj.com/vehicles/bike

]

I should be able to invalidate just cars because my page for cars have  
changed but not others.

Hope you got the point.

Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 9, 2012, 10:26am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/11 "2012-02-09T10:26:50Z")

</div>

Invalidate a cache page using the bypass mechanism proxy\_cache\_bypass,  
not  
expiration

To put it simply, Invalidate cache for a particular key entry but not  
all  
of the cache

e.g for

location = /vehicles {

proxy\_cache\_key $request\_uri;

}

Hashtable for cache:

[

[http://jj.com/vehicles/cars](http://jj.com/vehicles/cars) =\> cached page for cars

[http://jj.com/vehicles/bus](http://jj.com/vehicles/bus) =\> cached page for bus

[http://jj.com/vehicles/bike=](http://jj.com/vehicles/bike=)\> cached page for bike

- 10,000 key, values  
]

I should be able to invalidate just “cars” because my page for cars have  
changed but not others. Say a person added a comment in the cars page.  
Another analogy is to invalidate cache for a single blog post when all  
the  
blog posts have been cached under a single location directive.  
-Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 9, 2012, 12:59pm UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/12 "2012-02-09T12:59:53Z")

</div>

Thanks Antonio.

Great deal of help.

Looking into the wiki I see

“The following response headers flag a response as uncacheable unless  
they  
are ignored  
[http://wiki.nginx.org/HttpProxyModule#proxy\_ignore\_headers](http://wiki.nginx.org/HttpProxyModule#proxy_ignore_headers):

Set-Cookie

Cache-Control containing “no-cache”, “no-store”, “private”, or a  
“max-age”  
with a non-numeric or 0 value

Expires with a time in the past

X-Accel-Expires: 0“

This also means that if I send a curl call from my backend with a  
Cache-Control : max-age=1, then the corresponding request/page in cache  
will get invalidated the next second.

X-Accel-Expires also does the same thing I suppose. That solves my  
dilemma.

-Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [February 9, 2012, 11:14am UTC](https://www.ruby-forum.com/t/invalidate-just-one-page-under-a-location-directive/216570/13 "2012-02-09T11:14:08Z")

</div>

On 8 Fev 2012 16h22 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Invalidate a cache page using the bypass mechanism  
> proxy\_cache\_bypass, not expiration

IMHO the best option would be from the application side send a  
‘X-Accel-Expires: 0’ header. But this needs to be done at the _app  
level_.

Unless you place something in the headers that the application returns  
Nginx there’s no way of Nginx to know what is going on.

> To put it simply, Invalidate cache for a particular key entry but  
> not all of the cache

> [
> 
> [Site Maintenance](http://jj.com/vehicles/cars) =\> cached page for cars
> 
> [Site Maintenance](http://jj.com/vehicles/bus) =\> cached page for bus
> 
> [Site Maintenance](http://jj.com/vehicles/bike=)\> cached page for bike
> 
> - 10,000 key, values  
> ]

> I should be able to invalidate just “cars” because my page for cars  
> have changed but not others. Say a person added a comment in the  
> cars page. Another analogy is to invalidate cache for a single blog  
> post when all the blog posts have been cached under a single  
> location directive. -Quintin

Send a ‘X-Accel-Expires: 0’ header from the application at:

> **[Johnson & Johnson: Changing health for humanity](https://www.jnj.com/)**
>
> We’re tackling the world’s toughest health challenges

— appa
