Debugging FastCGI caching

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 bits of the config (and the full Nginx server{} config is
available
at 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:

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/

$ curl -i -I 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 and
Module ngx_http_fastcgi_module.

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. [email protected]

Francis D. Wrote:

$ curl -i -I 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 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:

Hello!

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

Thanks, Mono / 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).

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.