Skip to content

Instantly share code, notes, and snippets.

@SumonMSelim
Created October 24, 2016 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SumonMSelim/66ed1ccafab75ef231d485f9859c8a16 to your computer and use it in GitHub Desktop.
Save SumonMSelim/66ed1ccafab75ef231d485f9859c8a16 to your computer and use it in GitHub Desktop.
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