Different cache time for different locations

António P. P. Almeida appa at perusio.net
Thu Sep 20 21:56:21 UTC 2012


On 20 Set 2012 23h29 CEST, nfnlists at gmail.com wrote:

> Hello
>
> 2012/8/31 Maxim Dounin <mdounin at mdounin.ru>:
>> Hello!
>>
>> On Fri, Aug 31, 2012 at 05:46:02PM +0100, Nuno Neves wrote:
>>
>>> 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?
>>
>> 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 Dounin
>
> 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;
> ....
> }

Why do you use an insecure config? It's even referenced in the wiki: 
http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP

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

>
> 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
>
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx



More information about the nginx mailing list