# Debugging FastCGI caching

**URL:** <https://www.ruby-forum.com/t/debugging-fastcgi-caching/225736>\
**Category:** NGINX\
**Created:** [January 27, 2013, 1:13pm UTC](https://www.ruby-forum.com/t/debugging-fastcgi-caching/225736 "2013-01-27T13:13:30Z")\
**Posts on this page:** 4\
**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:** [January 27, 2013, 1:13pm UTC](https://www.ruby-forum.com/t/debugging-fastcgi-caching/225736/1 "2013-01-27T13:13:30Z")

</div>

I’m trying to use Nginx FastCGI caching to cache my site’s home page and  
blog home page. However, caching doesn’t seem to be working on these  
pages,  
and $upstream\_cache\_status always seems to be “MISS”.

Site URL: [http://dan.cx/](http://dan.cx/)

Relevant bits of the config (and the full Nginx server{} config is  
available  
at [https://github.com/Daniel15/Website/blob/master/nginx.conf](https://github.com/Daniel15/Website/blob/master/nginx.conf)):

* * *

# Handles only caching certain URLs

map $uri $dan\_no\_cache {  
default 1;  
~^/bundles/ 0;  
/ 0;  
/blog 0;  
}  
…

# Caching parameters

fastcgi\_cache\_key “$scheme$host$request\_uri”;  
fastcgi\_cache DANIEL15;  
fastcgi\_cache\_valid 60m;

# Don’t cache if $dan\_no\_cache map evaluates to 1

fastcgi\_cache\_bypass $dan\_no\_cache;  
fastcgi\_no\_cache $dan\_no\_cache;

# Add cache status as X-Cache header

add\_header X-Cache $upstream\_cache\_status;

* * *

Files under /bundles/ are correctly cached (see eg. the CSS and  
JavaScript  
on the site). I’ve noticed that $upstream\_cache\_status is “BYPASS” on  
pages  
that shouldn’t be cached, which makes me think it’s correctly picking up  
those URIs as being cacheable.

What is the best way to debug this issue?

Thanks!

Posted at Nginx Forum:

> **[Debugging FastCGI caching](https://forum.nginx.org/read.php?2,235609,235609#msg-235609)**
>
> I'm trying to use Nginx FastCGI caching to cache my site's home page and blog home page. However, caching doesn't seem to be working on these pages, and $upstream\_cache\_status always seems to be "MISS". Site URL: http://dan.cx/ Relevant...

---

<div class="post-metadata">

**Author:** ![Daniel15](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Daniel15](https://www.ruby-forum.com/u/Daniel15)\
**Post date:** [January 27, 2013, 1:32pm UTC](https://www.ruby-forum.com/t/debugging-fastcgi-caching/225736/2 "2013-01-27T13:32:47Z")

</div>

On Sun, Jan 27, 2013 at 07:12:59AM -0500, Daniel15 wrote:

Hi there,

> I’m trying to use Nginx FastCGI caching to cache my site’s home page and  
> blog home page. However, caching doesn’t seem to be working on these pages,  
> and $upstream\_cache\_status always seems to be “MISS”.
> 
> Site URL: [http://dan.cx/](http://dan.cx/)

$ curl -i -I [http://dan.cx/](http://dan.cx/)  
HTTP/1.1 200 OK  
…  
Cache-Control: private  
…

I don’t see anything in your config which adds that header, so  
presumably  
it is coming from upstream.

See [Module ngx\_http\_fastcgi\_module](http://nginx.org/r/fastcgi_cache_valid) and  
[Module ngx\_http\_fastcgi\_module](http://nginx.org/r/fastcgi_ignore_headers).

> What is the best way to debug this issue?

Examine the traffic between each client/server pair (in this case nginx  
and the fasctcgi server). Then protocol-specific knowledge might reveal  
the problem.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Daniel15](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Daniel15](https://www.ruby-forum.com/u/Daniel15)\
**Post date:** [January 28, 2013, 7:29am UTC](https://www.ruby-forum.com/t/debugging-fastcgi-caching/225736/3 "2013-01-28T07:29:21Z")

</div>

## Francis D. Wrote:

> $ curl -i -I [http://dan.cx/](http://dan.cx/)  
> HTTP/1.1 200 OK  
> …  
> Cache-Control: private  
> …
> 
> I don’t see anything in your config which adds that header, so  
> presumably it is coming from upstream.

Thanks, Mono / [ASP.NET](http://ASP.NET) must be adding that by default (as I have not  
explicitly added any caching headers). I’ll set the correct caching  
headers  
for this page.

Based on this, I assume that Nginx only caches responses with  
Cache-Control:  
public (or similar) headers? I couldn’t find this documented anywhere on  
the  
Nginx site so I wasn’t sure.

Does Nginx use the Expires or Max-Age headers, and how do these work  
with  
fastcgi\_cache\_valid? If the Max-Age is 1 hour and Expires is 1 hour in  
the  
future, but fastcgi\_cache\_valid is set to 2 hours, how long would Nginx  
cache the response for?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,235609,235630#msg-235630](http://forum.nginx.org/read.php?2,235609,235630#msg-235630)

---

<div class="post-metadata">

**Author:** ![Daniel15](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ecccb3/32.png) [@Daniel15](https://www.ruby-forum.com/u/Daniel15)\
**Post date:** [January 28, 2013, 9:52am UTC](https://www.ruby-forum.com/t/debugging-fastcgi-caching/225736/4 "2013-01-28T09:52:28Z")

</div>

Hello!

On Mon, Jan 28, 2013 at 01:28:36AM -0500, Daniel15 wrote:

> Thanks, Mono / [ASP.NET](http://ASP.NET) must be adding that by default (as I have not  
> explicitly added any caching headers). I’ll set the correct caching headers  
> for this page.
> 
> Based on this, I assume that Nginx only caches responses with Cache-Control:  
> public (or similar) headers? I couldn’t find this documented anywhere on the  
> Nginx site so I wasn’t sure.

Yes, any of the “private”, “no-cache” and “no-store” in  
Cache-Control disables cache.

> Does Nginx use the Expires or Max-Age headers, and how do these work with  
> fastcgi\_cache\_valid? If the Max-Age is 1 hour and Expires is 1 hour in the  
> future, but fastcgi\_cache\_valid is set to 2 hours, how long would Nginx  
> cache the response for?

The “Expires” header takes precedence over fastcgi\_cache\_valid  
(unless ignored with fastcgi\_ignore\_headers; and, BTW, this is  
documented at [Module ngx\_http\_fastcgi\_module](http://nginx.org/r/fastcgi_cache_valid)).

There is no “Max-Age” header, but the “max-age” directive of the  
“Cache-Control” header, and it takes precedence over  
fastcgi\_cache\_valid as well.

–  
Maxim D.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.
