=encoding utf-8 =head1 Name B - Transparent subrequest-based caching layout for arbitrary nginx locations I See L. This module is production ready. =head1 Version This document describes srcache-nginx-module L released on 15 May 2016. =head1 Synopsis upstream my_memcached { server 10.62.136.7:11211; keepalive 10; } location = /memc { internal; memc_connect_timeout 100ms; memc_send_timeout 100ms; memc_read_timeout 100ms; memc_ignore_client_abort on; set $memc_key $query_string; set $memc_exptime 300; memc_pass my_memcached; } location /foo { set $key $uri$args; srcache_fetch GET /memc $key; srcache_store PUT /memc $key; srcache_store_statuses 200 301 302; # proxy_pass/fastcgi_pass/drizzle_pass/echo/etc... # or even static files on the disk } location = /memc2 { internal; memc_connect_timeout 100ms; memc_send_timeout 100ms; memc_read_timeout 100ms; memc_ignore_client_abort on; set_unescape_uri $memc_key $arg_key; set $memc_exptime $arg_exptime; memc_pass unix:/tmp/memcached.sock; } location /bar { set_escape_uri $key $uri$args; srcache_fetch GET /memc2 key=$key; srcache_store PUT /memc2 key=$key&exptime=$srcache_expire; # proxy_pass/fastcgi_pass/drizzle_pass/echo/etc... # or even static files on the disk } map $request_method $skip_fetch { default 0; POST 1; PUT 1; } server { listen 8080; location /api/ { set $key "$uri?$args"; srcache_fetch GET /memc $key; srcache_store PUT /memc $key; srcache_methods GET PUT POST; srcache_fetch_skip $skip_fetch; # proxy_pass/drizzle_pass/content_by_lua/echo/... } } =head1 Description This module provides a transparent caching layer for arbitrary nginx locations (like those use an upstream or even serve static disk files). The caching behavior is mostly compatible with L. Usually, L is used together with this module to provide a concrete caching storage backend. But technically, any modules that provide a REST interface can be used as the fetching and storage subrequests used by this module. For main requests, the L directive works at the end of the access phase, so the L's L and L direcives run I ours, which is usually the desired behavior for security reasons. The workflow of this module looks like below: !L =head2 Subrequest caching For I, we explicitly B the use of this module because it's too difficult to get right. There used to be an implementation but it was buggy and I finally gave up fixing it and abandoned it. However, if you're using L, it's easy to do subrequest caching in Lua all by yourself. That is, first issue a subrequest to an L location to do an explicit cache lookup, if cache hit, just use the cached data returned; otherwise, fall back to the true backend, and finally do a cache insertion to feed the data into the cache. Using this module for main request caching and Lua for subrequest caching is the approach that we're taking in our business. This hybrid solution works great in production. =head2 Distributed Memcached Caching Here is a simple example demonstrating a distributed memcached caching mechanism built atop this module. Suppose we do have three different memcached nodes and we use simple modulo to hash our keys. http { upstream moon { server 10.62.136.54:11211; server unix:/tmp/memcached.sock backup; } upstream earth { server 10.62.136.55:11211; } upstream sun { server 10.62.136.56:11211; } upstream_list universe moon earth sun; server { memc_connect_timeout 100ms; memc_send_timeout 100ms; memc_read_timeout 100ms; location = /memc { internal; set $memc_key $query_string; set_hashed_upstream $backend universe $memc_key; set $memc_exptime 3600; # in seconds memc_pass $backend; } location / { set $key $uri; srcache_fetch GET /memc $key; srcache_store PUT /memc $key; # proxy_pass/fastcgi_pass/content_by_lua/drizzle_pass/... } } } Here's what is going on in the sample above: =over =item 1. We first define three upstreams, C, C, and C. These are our three memcached servers. =item 2. And then we group them together as an upstream list entity named C with the C directive provided by L. =item 3. After that, we define an internal location named C for talking to the memcached cluster. =item 4. In this C location, we first set the C<$memc_key> variable with the query string (C<$args>), and then use the L directive to hash our L<$memc_key|https://github.com/openresty/memc-nginx-module#memc_key> over the upsteam list C, so as to obtain a concrete upstream name to be assigned to the variable C<$backend>. =item 5. We pass this C<$backend> variable into the L directive. The C<$backend> variable can hold a value among C, C, and C. =item 6. Also, we define the memcached caching expiration time to be 3600 seconds (i.e., an hour) by overriding the L<$memc_exptime|https://github.com/openresty/memc-nginx-module#memc_exptime> variable. =item 7. In our main public location C, we configure the C<$uri> variable as our cache key, and then configure L for cache lookups and L for cache updates. We're using two subrequests to our C location defined earlier in these two directives. =back One can use L's L or L directives to inject custom Lua code to compute the C<$backend> and/or C<$key> variables in the sample above. One thing that should be taken care of is that memcached does have restriction on key lengths, i.e., 250 bytes, so for keys that may be very long, one could use the L directive or its friends to pre-hash the key to a fixed-length digest before assigning it to C<$memc_key> in the C location or the like. Further, one can utilize the L and L 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. So the possibility is really unlimited. To maximize speed, we often enable TCP (or Unix Domain Socket) connection pool for our memcached upstreams provided by L, for example, upstream moon { server 10.62.136.54:11211; server unix:/tmp/memcached.sock backup; keepalive 10; } where we define a connection pool which holds up to 10 keep-alive connections (per nginx worker process) for our C upstream (cluster). =head2 Caching with Redis One annoyance with Memcached backed caching is Memcached server's 1 MB value size limit. So it is often desired to use some more permissive backend storage services like Redis to serve as this module's backend. Here is a working example by using Redis: location /api { default_type text/css; set $key $uri; set_escape_uri $escaped_key $key; srcache_fetch GET /redis $key; srcache_store PUT /redis2 key=$escaped_key&exptime=120; # fastcgi_pass/proxy_pass/drizzle_pass/postgres_pass/echo/etc } location = /redis { internal; set_md5 $redis_key $args; redis_pass 127.0.0.1:6379; } location = /redis2 { internal; set_unescape_uri $exptime $arg_exptime; set_unescape_uri $key $arg_key; set_md5 $key; redis2_query set $key $echo_request_body; redis2_query expire $key $exptime; redis2_pass 127.0.0.1:6379; } This example makes use of the L<$echo_request_body|https://github.com/openresty/echo-nginx-module#echo_request_body> variable provided by L. Note that you need the latest version of L, C because earlier versions may not work reliably. Also, you need both L and L. The former is used in the L subrequest and the latter is used in the L subrequest. The Nginx core also has a bug that could prevent L's pipelining support from working properly in certain extreme conditions. And the following patch fixes this: http://mailman.nginx.org/pipermail/nginx-devel/2012-March/002040.html Note that, however, if you are using the L 1.0.15.3 bundle or later, then you already have everything that you need here in the bundle. =head2 Cache Key Preprocessing It is often desired to preprocess the cache key to exclude random noises that may hurt the cache hit rate. For example, random session IDs in the URI arguments are usually desired to get removed. Consider the following URI querystring SID=BC3781C3-2E02-4A11-89CF-34E5CFE8B0EF&UID=44332&L=EN&M=1&H=1&UNC=0&SRC=LK&RT=62 we want to remove the C and C arguments from it. It is easy to achieve if you use L at the same time: location = /t { rewrite_by_lua ' local args = ngx.req.get_uri_args() args.SID = nil args.UID = nil ngx.req.set_uri_args(args) '; echo $args; } Here we use the L directive from L to dump out the final value of L<$args|http://nginx.org/en/docs/http/ngx_http_core_module.html#var_args> in the end. You can replace it with your L configurations and upstream configurations instead for your case. Let's test this /t interface with curl: $ curl 'localhost:8081/t?RT=62&SID=BC3781C3-2E02-4A11-89CF-34E5CFE8B0EF&UID=44332&L=EN&M=1&H=1&UNC=0&SRC=LK' M=1&UNC=0&RT=62&H=1&L=EN&SRC=LK It is worth mentioning that, if you want to retain the order of the URI arguments, then you can do string substitutions on the value of L<$args|http://nginx.org/en/docs/http/ngx_http_core_module.html#var_args> directly, for example, location = /t { rewrite_by_lua ' local args = ngx.var.args newargs, n, err = ngx.re.gsub(args, [[\b[SU]ID=[^&]*&?]], "", "jo") if n and n > 0 then ngx.var.args = newargs end '; echo $args; } Now test it with the original curl command again, we get exactly what we would expect: RT=62&L=EN&M=1&H=1&UNC=0&SRC=LK But for caching purposes, it's good to normalize the URI argument order so that you can increase the cache hit rate. And the hash table entry order used by LuaJIT or Lua can be used to normalize the order as a nice side effect. =head1 Directives =head2 srcache_fetch B ImethodE EuriE EargsE?> B I B I B I This directive registers an access phase handler that will issue an Nginx subrequest to lookup the cache. When the subrequest returns status code other than C<200>, than a cache miss is signaled and the control flow will continue to the later phases including the content phase configured by L, L, and others. If the subrequest returns C<200 OK>, then a cache hit is signaled and this module will send the subrequest's response as the current main request's response to the client directly. This directive will always run at the end of the access phase, such that L's L and L will always run I this. You can use the L directive to disable cache look-up selectively. =head2 srcache_fetch_skip B IflagE> B I B I B I The C<< >> argument supports nginx variables. When this argument's value is not empty I not equal to C<0>, then the fetching process will be unconditionally skipped. For example, to skip caching requests which have a cookie named C with the value C, we can write location / { set $key ...; set_by_lua $skip ' if ngx.var.cookie_foo == "bar" then return 1 end return 0 '; srcache_fetch_skip $skip; srcache_store_skip $skip; srcache_fetch GET /memc $key; srcache_store GET /memc $key; # proxy_pass/fastcgi_pass/content_by_lua/... } where L is used to calculate the value of the C<$skip> variable at the (earlier) rewrite phase. Similarly, the C<$key> variable can be computed by Lua using the L or L directive too. The standard L directive can also be used to compute the value of the C<$skip> variable used in the sample above: map $cookie_foo $skip { default 0; bar 1; } but your L statement should be put into the C config block in your C file though. =head2 srcache_store B ImethodE EuriE EargsE?> B I B I B I This directive registers an output filter handler that will issue an Nginx subrequest to save the response of the current main request into a cache backend. The status code of the subrequest will be ignored. You can use the L and L directives to disable caching for certain requests in case of a cache miss. Since the C release, both the response status line, response headers, and response bodies will be put into the cache. By default, the following special response headers will not be cached: =over =item * Connection =item * Keep-Alive =item * Proxy-Authenticate =item * Proxy-Authorization =item * TE =item * Trailers =item * Transfer-Encoding =item * Upgrade =item * Set-Cookie =back You can use the L and/or L directives to control what headers to cache and what not. The original response's data chunks get emitted as soon as they arrive. C just copies and collects the data in an output filter without postponing them from being sent downstream. But please note that even though all the response data will be sent immediately, the current Nginx request lifetime will not finish until the srcache_store subrequest completes. That means a delay in closing the TCP connection on the server side (when HTTP keepalive is disabled, but proper HTTP clients should close the connection actively on the client side, which adds no extra delay or other issues at all) or serving the next request sent on the same TCP connection (when HTTP keepalive is in action). =head2 srcache_store_max_size B IsizeE> B I B I B I When the response body length is exceeding this size, this module will not try to store the response body into the cache using the subrequest template that is specified in L. This is particular useful when using cache storage backend that does have a hard upper limit on the input data. For example, for Memcached server, the limit is usually C<1 MB>. When C<0> is specified (the default value), there's no limit check at all. =head2 srcache_store_skip B IflagE> B I B I B I The C<< >> argument supports Nginx variables. When this argument's value is not empty I not equal to C<0>, then the storing process will be unconditionally skipped. Starting from the C release, the C<< >> expression (possibly containing Nginx variables) can be evaluated up to twice: the first time is right after the response header is being sent and when the C<< >> expression is not evaluated to true values it will be evaluated again right after the end of the response body data stream is seen. Before C, only the first time evaluation is performed. Here's an example using Lua to set $nocache to avoid storing URIs that contain the string "/tmp": set_by_lua $nocache ' if string.match(ngx.var.uri, "/tmp") then return 1 end return 0'; srcache_store_skip $nocache; =head2 srcache_store_statuses B Istatus1E Estatus2E ..> B I B I B I This directive controls what responses to store to the cache according to their status code. By default, only C<200>, C<301>, and C<302> responses will be stored to cache and any other responses will skip L. You can specify arbitrary positive numbers for the response status code that you'd like to cache, even including error code like C<404> and C<503>. For example: srcache_store_statuses 200 201 301 302 404 503; At least one argument should be given to this directive. This directive was first introduced in the C release. =head2 srcache_store_ranges B I B I B I B I When this directive is turned on (default to C), L will also store 206 Partial Content responses generated by the standard C. If you turn this directive on, you MUST add C<$http_range> to your cache keys. For example, location / { set $key "$uri$args$http_range"; srcache_fetch GET /memc $key; srcache_store PUT /memc $key; } This directive was first introduced in the C release. =head2 srcache_header_buffer_size B IsizeE> B I B I B I This directive controles the header buffer when serializing response headers for L. The default size is the page size, usually C<4k> or C<8k> depending on specific platforms. Note that the buffer is not used to hold all the response headers, but just each individual header. So the buffer is merely needed to be big enough to hold the longest response header. This directive was first introduced in the C release. =head2 srcache_store_hide_header B IheaderE> B I B I B I By default, this module caches all the response headers except the following ones: =over =item * Connection =item * Keep-Alive =item * Proxy-Authenticate =item * Proxy-Authorization =item * TE =item * Trailers =item * Transfer-Encoding =item * Upgrade =item * Set-Cookie =back You can hide even more response headers from L by listing their names (case-insensitive) by means of this directive. For examples, srcache_store_hide_header X-Foo; srcache_store_hide_header Last-Modified; Multiple occurrences of this directive are allowed in a single location. This directive was first introduced in the C release. See also L. =head2 srcache_store_pass_header B IheaderE> B I B I B I By default, this module caches all the response headers except the following ones: =over =item * Connection =item * Keep-Alive =item * Proxy-Authenticate =item * Proxy-Authorization =item * TE =item * Trailers =item * Transfer-Encoding =item * Upgrade =item * Set-Cookie =back You can force L to store one or more of these response headers from L by listing their names (case-insensitive) by means of this directive. For examples, srcache_store_pass_header Set-Cookie; srcache_store_pass_header Proxy-Autenticate; Multiple occurrences of this directive are allowed in a single location. This directive was first introduced in the C release. See also L. =head2 srcache_methods B ImethodE...> B I B I B I This directive specifies HTTP request methods that are considered by either L or L. HTTP request methods not listed will be skipped completely from the cache. The following HTTP methods are allowed: C, C, C, C, and C. The C and C methods are always implicitly included in the list regardless of their presence in this directive. Note that since the C release C requests are always skipped by L because their responses never carry a response body. This directive was first introduced in the C release. =head2 srcache_ignore_content_encoding B I B I B I B I When this directive is turned C (which is the default), non-empty C response header will cause L skip storing the whole response into the cache and issue a warning into nginx's C file like this: [warn] 12500#0: *1 srcache_store skipped due to response header "Content-Encoding: gzip" (maybe you forgot to disable compression on the backend?) Turning on this directive will ignore the C response header and store the response as usual (and also without warning). It's recommended to always disable gzip/deflate compression on your backend server by specifying the following line in your C file: proxy_set_header Accept-Encoding ""; This directive was first introduced in the C release. =head2 srcache_request_cache_control B I B I B I B I When this directive is turned C, the request headers C and C will be honored by this module in the following ways: =over =item 1. L, i.e., the cache lookup operation, will be skipped when request headers C and/or C are present. =item 2. L, i.e., the cache store operation, will be skipped when the request header C is specified. =back Turning off this directive will disable this functionality and is considered safer for busy sites mainly relying on cache for speed. This directive was first introduced in the C release. See also L. =head2 srcache_response_cache_control B I B I B I B I When this directive is turned C, the response headers C and C will be honored by this module in the following ways: =over =item * C skips L, =item * C skips L, =item * C skips L, =item * C skips L, =item * and C<< Expires: >> skips L. =back This directive takes priority over the L, L, and L directives. This directive was first introduced in the C release. See also L. =head2 srcache_store_no_store B I B I B I B I Turning this directive on will force responses with the header C to be stored into the cache when L is turned C I other conditions are met. Default to C. This directive was first introduced in the C release. =head2 srcache_store_no_cache B I B I B I B I Turning this directive on will force responses with the header C to be stored into the cache when L is turned C I other conditions are met. Default to C. This directive was first introduced in the C release. =head2 srcache_store_private B I B I B I B I Turning this directive on will force responses with the header C to be stored into the cache when L is turned C I other conditions are met. Default to C. This directive was first introduced in the C release. =head2 srcache_default_expire B ItimeE> B I B I B I This directive controls the default expiration time period that is allowed for the L<$srcache_expire> variable value when neither C nor C are specified in the response headers. The C<<