Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
nginx fastcgi cache
### SET THESE OUTSIDE `server` BLOCK ###
# set cache path, key and expiration time
fastcgi_cache_path /tmp/cache levels=1:2 keys_zone=MYAPP:512m inactive=60m;
# handle nginx crash, timeout, error responses and serve cached data
fastcgi_cache_use_stale error timeout invalid_header http_500;
# ignore cache headers
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
### SET THESE INSIDE `server` BLOCK ###
# set cache
set $no_cache 0;
# disable caching for requests other than GET
if ($request_method != GET) {
set $no_cache 1; # disable caching
}
# set cache key
fastcgi_cache_key "$scheme$request_method$host$uri";
### SET THESE INSIDE `location ~ \.php$` BLOCK ###
fastcgi_cache MYAPP;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
fastcgi_cache_valid 200 5m; # cache validation time
add_header X-Fastcgi-Cache $upstream_cache_status;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment