How to tell nginx to honor backend's cache?

Posted by ChocoDeveloper on Server Fault See other posts from Server Fault or by ChocoDeveloper
Published on 2012-12-01T15:01:06Z Indexed on 2012/12/01 17:05 UTC
Read the original article Hit count: 222

Filed under:
|
|

I'm using php-fpm with nginx as http server (I don't know much about reverse proxies, I just installed it and didn't touch anything), without Apache nor Varnish.

I need nginx to understand and honor the http headers I send. I tried with this config (taken from the docs) but didn't work:

/etc/nginx/nginx.conf:

fastcgi_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=website:10m inactive=10m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

/etc/nginx/sites-available/website:

server {
    fastcgi_cache website;
    #fastcgi_cache_valid 200 302 1h;
    #fastcgi_cache_valid 301     1d;
    #fastcgi_cache_valid any     1m;
    #fastcgi_cache_min_uses 1;
    #fastcgi_cache_use_stale error timeout invalid_header http_503;
    add_header X-Cache $upstream_cache_status;
}

I always get "MISS" and the cache dir is empty. If I uncomment the other directives, I get hit, but I don't want those "dumb" settings, I need to control them within my backend. For example, if my backend says "public, s-maxage=10", the cache should be considered stale after 10 secs. Instead, nginx will store it for 1h, because of these directives.

I was thinking whether I should try proxy_cache, not sure what's the difference. In both fastcgi and proxy modules docs it says this:

The cache honors backend's Cache-Control, Expires, and etc.
since version 0.7.48, Cache-Control: private and no-store only
since 0.7.66, though. Vary handling is not implemented.

nginx version: nginx/1.1.19

Any thoughts?

pd: I also have the reverse proxy that is offered by Symfony2 (which I turn off to use nginx's). The headers are interpreted correctly by it, so I think I'm doing it right.

© Server Fault or respective owner

Related posts about nginx

Related posts about http