# Caching fastcgi url

**URL:** <https://www.ruby-forum.com/t/caching-fastcgi-url/242433>\
**Category:** NGINX\
**Created:** [June 23, 2015, 10:20pm UTC](https://www.ruby-forum.com/t/caching-fastcgi-url/242433 "2015-06-23T22:20:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [June 23, 2015, 10:20pm UTC](https://www.ruby-forum.com/t/caching-fastcgi-url/242433/1 "2015-06-23T22:20:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![CJ\_Ess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@CJ\_Ess](https://www.ruby-forum.com/u/CJ_Ess)\
**Post date:** [June 24, 2015, 12:08am UTC](https://www.ruby-forum.com/t/caching-fastcgi-url/242433/2 "2015-06-24T00:08:29Z")

</div>

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](http://nginx.org/r/fastcgi_cache)

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.

> **[Module ngx\_http\_fastcgi\_module](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache_use_stale)**
>
> The ngx\_http\_fastcgi\_module module allows passing requests to a FastCGI server.

> **[Module ngx\_http\_fastcgi\_module](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache_lock)**
>
> The ngx\_http\_fastcgi\_module module allows passing requests to a FastCGI server.

> - 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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![CJ\_Ess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@CJ\_Ess](https://www.ruby-forum.com/u/CJ_Ess)\
**Post date:** [June 24, 2015, 12:28am UTC](https://www.ruby-forum.com/t/caching-fastcgi-url/242433/3 "2015-06-24T00:28:21Z")

</div>

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](https://gist.github.com/magnetikonline/10450786)) 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?

---

<div class="post-metadata">

**Author:** ![CJ\_Ess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@CJ\_Ess](https://www.ruby-forum.com/u/CJ_Ess)\
**Post date:** [June 24, 2015, 6:38pm UTC](https://www.ruby-forum.com/t/caching-fastcgi-url/242433/4 "2015-06-24T18:38:33Z")

</div>

On Tue, Jun 23, 2015 at 6:27 PM CJ Ess [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > only the one matching /xyz is to be cached?
> > 
> > If that’s what you want, you can probably configure it.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

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.

---

<div class="post-metadata">

**Author:** ![CJ\_Ess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@CJ\_Ess](https://www.ruby-forum.com/u/CJ_Ess)\
**Post date:** [June 24, 2015, 7:46pm UTC](https://www.ruby-forum.com/t/caching-fastcgi-url/242433/5 "2015-06-24T19:46:55Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![CJ\_Ess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@CJ\_Ess](https://www.ruby-forum.com/u/CJ_Ess)\
**Post date:** [June 24, 2015, 12:40am UTC](https://www.ruby-forum.com/t/caching-fastcgi-url/242433/6 "2015-06-24T00:40:02Z")

</div>

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](https://gist.github.com/magnetikonline/10450786)) 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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
