http-srcache-filter #

Transparent subrequest-based response caching — store and serve cached output via any backend nginx can talk to (memcached, redis), bypassing the built-in proxy/fastcgi cache.

Source: upstream source

Directives

srcache_buffer #

syntax: srcache_buffer 1 arg (size (k/m/g));  ·  context: http, server, location, location-if

Size in bytes; accepts k / m / g suffixes.

srcache_default_expire #

syntax: srcache_default_expire 1 arg (duration in seconds);  ·  context: http, server, location, location-if

Duration in seconds; accepts s / m / h / d suffixes.

srcache_fetch #

syntax: srcache_fetch 2 args;  ·  context: http, server, location, location-if

For main requests, the srcache_fetch directive works at the end of the access phase, so the standard access module's allow and deny direcives run before ours, which is usually the desired behavior for security reasons.

srcache_fetch_skip #

syntax: srcache_fetch_skip 1 arg;  ·  context: http, server, location, location-if

Further, one can utilize the srcache_fetch_skip and srcache_store_skip directives to control what to cache and what not on a per-request basis, and Lua can also be used here in a similar way.

srcache_header_buffer_size #

syntax: srcache_header_buffer_size 1 arg (size (k/m/g));  ·  context: http, server, location, location-if

Size in bytes; accepts k / m / g suffixes.

srcache_ignore_content_encoding #

syntax: srcache_ignore_content_encoding on | off (on/off flag);  ·  context: http, server, location

Boolean directive — set to "on" or "off".

srcache_max_expire #

syntax: srcache_max_expire 1 arg (duration in seconds);  ·  context: http, server, location, location-if

The final value of this variable will be the value specified by the srcache_max_expire directive if the value obtained in the algorithm above exceeds the maximal value (if any).

srcache_methods #

syntax: srcache_methods 1+ args (bitmask);  ·  context: http, server, location

See also srcache_store_hide_header. Back to TOC srcache_methods --------------- syntax: srcache_methods <method>... default: srcache_methods GET HEAD context: http, server, location phase: post-access, output-header-filter This directive specifies HTTP request methods that are considered by either srcache_fetch or srcache_store.

srcache_request_cache_control #

syntax: srcache_request_cache_control on | off (on/off flag);  ·  context: http, server, location

Boolean directive — set to "on" or "off".

srcache_response_cache_control #

syntax: srcache_response_cache_control on | off (on/off flag);  ·  context: http, server, location

Boolean directive — set to "on" or "off".

srcache_store #

syntax: srcache_store 2 args;  ·  context: http, server, location, location-if

In our main public location /, we configure the $uri variable as our cache key, and then configure srcache_fetch for cache lookups and srcache_store for cache updates.

srcache_store_hide_header #

syntax: srcache_store_hide_header 1 arg (list of strings);  ·  context: http, server, location

By default, the following special response headers will not be cached: Connection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailers Transfer-Encoding Upgrade * Set-Cookie You can use the srcache_store_pass_header and/or srcache_store_hide_header directives to control what headers to cache and what not.

srcache_store_max_size #

syntax: srcache_store_max_size 1 arg (size (k/m/g));  ·  context: http, server, location, location-if

You can use the srcache_store_skip and srcache_store_max_size directives to disable caching for certain requests in case of a cache miss.

srcache_store_no_cache #

syntax: srcache_store_no_cache on | off (on/off flag);  ·  context: http, server, location

This directive takes priority over the srcache_store_no_store, srcache_store_no_cache, and srcache_store_private directives.

srcache_store_no_store #

syntax: srcache_store_no_store on | off (on/off flag);  ·  context: http, server, location

This directive takes priority over the srcache_store_no_store, srcache_store_no_cache, and srcache_store_private directives.

srcache_store_pass_header #

syntax: srcache_store_pass_header 1 arg (list of strings);  ·  context: http, server, location

By default, the following special response headers will not be cached: Connection Keep-Alive Proxy-Authenticate Proxy-Authorization TE Trailers Transfer-Encoding Upgrade * Set-Cookie You can use the srcache_store_pass_header and/or srcache_store_hide_header directives to control what headers to cache and what not.

srcache_store_private #

syntax: srcache_store_private on | off (on/off flag);  ·  context: http, server, location

This directive takes priority over the srcache_store_no_store, srcache_store_no_cache, and srcache_store_private directives.

srcache_store_ranges #

syntax: srcache_store_ranges on | off (on/off flag);  ·  context: http, server, location

Boolean directive — set to "on" or "off".

srcache_store_skip #

syntax: srcache_store_skip 1 arg;  ·  context: http, server, location, location-if

Further, one can utilize the srcache_fetch_skip and srcache_store_skip directives to control what to cache and what not on a per-request basis, and Lua can also be used here in a similar way.

srcache_store_statuses #

syntax: srcache_store_statuses 1+ args;  ·  context: http, server, location, location-if

Specifies which response status codes are eligible to be stored by srcache_store, defaulting to 200, 301, 302, 307, and 308.

Example

  location ~ '\.php$|^/update.php' {
    # cache setup
    set $key $request_uri;
    try_files $uri =404;

    srcache_fetch_skip $skip_cache;
    srcache_store_skip $skip_cache;

    srcache_response_cache_control off;
    srcache_store_statuses 200 201 301 302 307 308 404 503;

    set_escape_uri $escaped_key $key;

    srcache_fetch GET /redis-fetch $key;
    srcache_store PUT /redis-store key=$escaped_key;

    more_set_headers 'X-Cache-Fetch-Status $srcache_fetch_status';
    more_set_headers 'X-Cache-Store-Status $srcache_store_status';

    fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
    # Security note: If you're running a version of PHP older than the
    # latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
    # See http://serverfault.com/q/627903/94922 for details.
    include fastcgi_params;
    # Block httproxy attacks. See https://httpoxy.org/.
    fastcgi_param HTTP_PROXY "";
    fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_intercept_errors on;

    fastcgi_pass upstream-name;
  }

  location /redis-fetch {
    internal;

    resolver 8.8.8.8 valid=300s;
    resolver_timeout 10s;

    content_by_lua_block {
      local key = assert(ngx.var.request_uri, "no key found")
      local redis = require "resty.redis"
      local red, err = redis:new()
      if not red then
        ngx.log(ngx.ERR, "Failed to create redis variable, error -> ", err)
        ngx.exit(500)
      end
      assert(red:connect("redis-master.default.svc.cluster.local", 6379))
      if not red then
        ngx.log(ngx.ERR, "Failed to connect to redis, error -> ", err)
        ngx.exit(500)
      end
      local res, err = red:auth("redispassword")
      if not res then
        ngx.say("failed to authenticate, ", err)
        ngx.exit(500)
      end
      local data = assert(red:get(key))
      assert(red:set_keepalive(10000, 100))
      if res == ngx.null then
        return ngx.exit(404)
      end
      ngx.print(data)
    }
  }

  location /redis-store {
    internal;

    resolver 8.8.8.8 valid=300s;
    resolver_timeout 10s;

    content_by_lua_block {
      local value = assert(ngx.req.get_body_data(), "no value found")
      local key = assert(ngx.var.request_uri, "no key found")
      local redis = require "resty.redis"
      local
…

↑ back to index