Forum: NGINX ignore part of URL to force caching?

Posted by AJ Weber (Guest)
on 2012-10-04 20:21
(Received via mailing list)
I would like to "override" the intent of the app server that is
basically disabling any caching of the backend file.  For example, they
are embedding a "noCache=#######" parameter at the end of the URL (there
are other parameters following, but if I can check the url up-to the "?"
that would suit me fine).

This is actually a dynamically generated SWF file, but the file is then
constant for a reasonable amount of time such that I'd like to cache it
for a few minutes.

Is there a way in a specific "location" to tell nginx to ignore the
parameters (or any portion of the URL) when determining the cached
object for that URL?  In other words, tell nginx to cache content for
that location, say only 5min, and ignore all parameters when determining
whether to cache and how to match cached content?

If I'm not explaining myself properly, please let me know and I'll try
another route. :)

Thanks in advance,
AJ
Posted by "António P. P. Almeida" <appa@perusio.net> (Guest)
on 2012-10-05 11:50
(Received via mailing list)
On 4 Out 2012 20h20 CEST, aweber@comcast.net wrote:

> I would like to "override" the intent of the app server that is
> basically disabling any caching of the backend file.  For example,
> they are embedding a "noCache=#######" parameter at the end of the
> URL (there are other parameters following, but if I can check the
> url up-to the "?"  that would suit me fine).
>
> This is actually a dynamically generated SWF file, but the file is
> then constant for a reasonable amount of time such that I'd like to
> cache it for a few minutes.

> Is there a way in a specific "location" to tell nginx to ignore the
> parameters (or any portion of the URL) when determining the cached
> object for that URL?  In other words, tell nginx to cache content
> for that location, say only 5min, and ignore all parameters when
> determining whether to cache and how to match cached content?

If I understand correctly. Inside your main cached location you can do:

if ($request_uri ~* noCache) {
   return 418;
}

error_page 418 =200 @other-cached-location;

And add @other-cached-location:

location @other-cached-location {
    # Set your cache parameters here and serve the content.
}

--- appa
Posted by Igor Sysoev (Guest)
on 2012-10-05 12:08
(Received via mailing list)
On Thu, Oct 04, 2012 at 02:20:30PM -0400, AJ Weber wrote:
> Is there a way in a specific "location" to tell nginx to ignore the
> parameters (or any portion of the URL) when determining the cached
> object for that URL?  In other words, tell nginx to cache content for
> that location, say only 5min, and ignore all parameters when determining
> whether to cache and how to match cached content?
>
> If I'm not explaining myself properly, please let me know and I'll try
> another route. :)

You can use "proxy_cache_key":
http://nginx.org/en/docs/http/ngx_http_proxy_modul...

For example, to completly ignore query strings:

location /swf/ {
    ...
    proxy_cache  ...
    proxy_cache_valid  5m;
    proxy_cache_key    $proxy_host$uri;
}

or to account query strings parameters ONE and TWO:

location /swf/ {
    ...
    proxy_cache  ...
    proxy_cache_valid  5m;
    proxy_cache_key    $proxy_host$uri?$arg_ONE&$arg_TWO;
}


--
Igor Sysoev
http://nginx.com/support.html
Posted by Igor Sysoev (Guest)
on 2012-10-05 12:09
(Received via mailing list)
On Fri, Oct 05, 2012 at 11:49:12AM +0200, Antnio P. P. Almeida wrote:
> > cache it for a few minutes.
>    return 418;
> }
>
> error_page 418 =200 @other-cached-location;
>
> And add @other-cached-location:
>
> location @other-cached-location {
>     # Set your cache parameters here and serve the content.
> }

There is proxy_cache_key for this.


--
Igor Sysoev
http://nginx.com/support.html
Posted by "António P. P. Almeida" <appa@perusio.net> (Guest)
on 2012-10-05 12:20
(Received via mailing list)
On 5 Out 2012 12h09 CEST, igor@sysoev.ru wrote:

>>> This is actually a dynamically generated SWF file, but the file is
>> If I understand correctly. Inside your main cached location you can
>> location @other-cached-location {
>> # Set your cache parameters here and serve the content.
>> }
>
> There is proxy_cache_key for this.

I was under the impression that there wasn't a specific location for
the swf files. If there is in fact the proxy already has all you need
for doing it much easier.

--- appa
Posted by AJ Weber (Guest)
on 2012-10-05 17:46
(Received via mailing list)
This worked exactly as Igor described.

Thank you!
-AJ
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.