# Different cache time for different locations

**URL:** <https://www.ruby-forum.com/t/different-cache-time-for-different-locations/222529>\
**Category:** NGINX\
**Created:** [August 31, 2012, 6:46pm UTC](https://www.ruby-forum.com/t/different-cache-time-for-different-locations/222529 "2012-08-31T18:46:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 31, 2012, 6:46pm UTC](https://www.ruby-forum.com/t/different-cache-time-for-different-locations/222529/1 "2012-08-31T18:46:29Z")

</div>

Hello,

All my requests passes in the php location to fastcgi ( location ~  
.php$ ) and I would like to have different cache time for different  
requests.

As an example :

/ -\> cache time for 10 minutes  
/about -\> cache time for 1 day  
/products -\> cache time for 1h

Right now I have the same cache for all requests: fastcgi\_cache\_valid  
200 302 10m;

How am I able to accomplish this setup?

Thanks

Nuno

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [August 31, 2012, 7:08pm UTC](https://www.ruby-forum.com/t/different-cache-time-for-different-locations/222529/2 "2012-08-31T19:08:41Z")

</div>

Hello!

On Fri, Aug 31, 2012 at 05:46:02PM +0100, Nuno N. wrote:

> /products → cache time for 1h
> 
> Right now I have the same cache for all requests: fastcgi\_cache\_valid  
> 200 302 10m;
> 
> How am I able to accomplish this setup?

Configure different locations to handle different requests, for  
example

location = / {  
fastcgi\_pass …  
fastcgi\_cache\_valid 10m;  
…  
}

location = /about {  
fastcgi\_pass …  
fastcgi\_cache\_valid 1d;  
…  
}

location = /products {  
fastcgi\_pass …  
fastcgi\_cache\_valid 1h;  
…  
}

Maxim D.

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [September 20, 2012, 11:35pm UTC](https://www.ruby-forum.com/t/different-cache-time-for-different-locations/222529/3 "2012-09-20T23:35:52Z")

</div>

Hello

2012/8/31 Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > As an example :  
> > Configure different locations to handle different requests, for  
> > fastcgi\_cache\_valid 1d;  
> > …  
> > }
> 
> location = /products {  
> fastcgi\_pass …  
> fastcgi\_cache\_valid 1h;  
> …  
> }
> 
> Maxim D.

I’ve tested this configuration, but since all php requests pass in (  
location ~ .php$ ) and all requests are sent to /index.php with  
try\_files, this doesn’t work.

For example, all requests to /products are handle with /index.php with  
try\_files

location / {  
try\_files $uri $uri/ /index.php;  
}

location ~ .php$ {  
fastcgi\_pass …  
fastcgi\_cache one;  
fastcgi\_cache\_valid 200 20m;  
…  
}

If I add:

location = /products {  
fastcgi\_pass …  
fastcgi\_cache\_valid 1h;  
…  
}

A request to /products get the value from .php location (  
fastcgi\_cache\_valid 200 10m; )  
If I remove fastcgi\_cache\_valid from .php location, all requests I  
get are MISS.

Any ideas to accomplished this?

Thanks

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [September 20, 2012, 11:56pm UTC](https://www.ruby-forum.com/t/different-cache-time-for-different-locations/222529/4 "2012-09-20T23:56:54Z")

</div>

On 20 Set 2012 23h29 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > .php$ ) and I would like to have different cache time for
> > 
> > }  
> > location / {  
> > try\_files $uri $uri/ /index.php;  
> > }
> 
> location ~ .php$ {  
> fastcgi\_pass …  
> fastcgi\_cache one;  
> fastcgi\_cache\_valid 200 20m;  
> …  
> }

Why do you use an insecure config? It’s even referenced in the wiki:

> **[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

I’m aware that is quite common to “recommend” this as de rigueur for  
PHP but I fail to see why, It seems that is just easier to have a  
catch all php than enumerate all scripts that are allowed.

Maxim suggestion doesn’t work for you because you have this regex  
based location. I suggest you formulate your problem more clearly so  
that the list can help you.

Start by trying an exact location for index.php

location = /index.php {  
…  
}

location = /products {  
…  
}

You have to “enable” PHP FCGI handling in each location.

— appa

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [September 21, 2012, 6:45pm UTC](https://www.ruby-forum.com/t/different-cache-time-for-different-locations/222529/5 "2012-09-21T18:45:20Z")

</div>

On 21 Set 2012 15h40 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> That’s the way applications like wordpress and others works, in my  
> case it’s Invision Power Board.

With WP you can enumerate all PHP files and even if you don’t want to  
do that you can constrain the execution by using something like this:

```
    ## Regular PHP processing.
    location ~ ^(?<script>.+\.php)(?<path_info>.*)$ {
        include fastcgi.conf;
        ## The fastcgi_params must be redefined from the ones
        ## given in fastcgi.conf. No longer standard names
        ## but arbitrary: named patterns in regex.
        fastcgi_param SCRIPT_FILENAME $document_root$script;
        fastcgi_param SCRIPT_NAME $script;
        fastcgi_param PATH_INFO $path_info;
        ## Passing the request upstream to the FastCGI
        ## listener.
        fastcgi_pass phpcgi;
    }

```

— appa

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [September 21, 2012, 3:41pm UTC](https://www.ruby-forum.com/t/different-cache-time-for-different-locations/222529/6 "2012-09-21T15:41:12Z")

</div>

Hello,

That’s the way applications like wordpress and others works, in my  
case it’s Invision Power Board.

Thanks anyway.

Nuno

2012/9/20 Antnio P. P. Almeida [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
