Caching fastcgi url

Hello,

I am looking for advice. I am using nginx to terminate SSL and forward
the
request to php via fastcgi. Of all of requests I am forwarding to
fastcgi
there is one particular URL that I want to cache, hopefully bypassing
communication with the fastcgi and php processes altogether.

  • Would I need to define a separate location stanza for the URL I want
    to
    cache and duplicate all of the fastcgi configuration that is normally
    required? Or is there a way to indicate that of all the fastcgi requests
    only the one matching /xyz is to be cached?

  • If multiple request for the same URL arrive at around the same time,
    and
    the cache is stale, they will all wait on the one request that is
    refreshing the cache, correct? So I should only see one request for the
    cached location per worker per minute on the backend?

  • Since my one URI is fairly small, can I indicate that no file backing
    is
    needed?

On Tue, Jun 23, 2015 at 04:19:48PM -0400, CJ Ess wrote:

Hi there,

  • Would I need to define a separate location stanza for the URL I want to
    cache and duplicate all of the fastcgi configuration that is normally
    required? Or is there a way to indicate that of all the fastcgi requests
    only the one matching /xyz is to be cached?

fastcgi caching is handled by the fastcgi_cache directive, documented
at Module ngx_http_fastcgi_module

It is set per-location.

See also directives like fastcgi_cache_bypass and fastcgi_no_cache.

It is probably simplest to have on exact-match location for this url
and not worry about the no_cache side of things.

“all the configuration that is normally required” is typically four
lines
– one “include” of common stuff; one or two extra fastcgi_param values,
and a fastcgi_pass.

  • If multiple request for the same URL arrive at around the same time, and
    the cache is stale, they will all wait on the one request that is
    refreshing the cache, correct? So I should only see one request for the
    cached location per worker per minute on the backend?

If that’s what you want, you can probably configure it.

http://nginx.org/r/fastcgi_cache_use_stale
http://nginx.org/r/fastcgi_cache_lock

  • Since my one URI is fairly small, can I indicate that no file backing is
    needed?

I don’t think so. But you can have fastcgi_cache_path set to a ramdisk,
I think.

f

Francis D. [email protected]

So looks like your saying the best way to do it is to do a separate
location and duplicate the fastcgi setup in that location and add the
fastcgi_cache stuff.

I can work with that, however I came across this example while googling
(
Nginx FastCGI cache configuration example. · GitHub) that uses “if” to set a
variable which I could use to match on the URL and trigger
fastcgi_cache_bypass for everything not matching. Is “if” so toxic that
I
shouldn’t consider doing it this way?

On Tue, Jun 23, 2015 at 6:27 PM CJ Ess [email protected] wrote:

only the one matching /xyz is to be cached?

If that’s what you want, you can probably configure it.


nginx mailing list
[email protected]
nginx Info Page

For short, if is evil. If you are not sure, don’t use.

If you don’t have other fastcgi locations, you can do fastcgi setup in
server
block. It’s fine to set fastcgi parameters, they won’t affect static
requests until you do fastcgi_pass.

Everything is fastcgi, my question is how best to treat one single
fastcgi
URL differently (caching it instead of forwarding every request to the
backend).

On Tue, Jun 23, 2015 at 06:27:30PM -0400, CJ Ess wrote:

Hi there,

So looks like your saying the best way to do it is to do a separate
location and duplicate the fastcgi setup in that location and add the
fastcgi_cache stuff.

It strikes me as clearer to have one extra location{} than to have a
map{} or an if() to set a variable and then use fastcgi_no_cache and/or
fastcgi_cache_bypass with that variable.

But either way should work.

I’m not going to be the one trying to understand it in a year’s time.

I can work with that, however I came across this example while googling (
Nginx FastCGI cache configuration example. · GitHub) that uses “if” to set a
variable which I could use to match on the URL and trigger
fastcgi_cache_bypass for everything not matching. Is “if” so toxic that I
shouldn’t consider doing it this way?

“if” seems fine for that, so long as it is not inside location{}.

I’d probably use “map”, though, if I were going to do that.

f

Francis D. [email protected]