Skip to content

Commit 0aee311

Browse files
committed
Add support for page cache using Redis
1 parent 87a009e commit 0aee311

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

php/docker/templates/nginx-vhost-conf.d/30-well-knonw-locations.conf renamed to php/docker/templates/nginx-vhost-conf.d/30-well-known-locations.conf

File renamed without changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# vim: set ft=nginx:
2+
3+
{{- if eq .Env.STACK_PAGE_CACHE_BACKEND "redis" }}
4+
location /.stack-cache-fetch {
5+
internal;
6+
7+
set $redis_key $args;
8+
redis_pass {{ default "localhost:6379" .Env.STACK_PAGE_CACHE_REDIS_HOST_PATH }};
9+
}
10+
11+
location /.stack-cache-store {
12+
internal;
13+
14+
set_unescape_uri $exptime $arg_exptime;
15+
set_unescape_uri $key $arg_key;
16+
17+
redis2_query set $key $echo_request_body;
18+
redis2_query expire $key $exptime;
19+
redis2_pass {{ default "localhost:6379" .Env.STACK_PAGE_CACHE_REDIS_HOST_PATH }};
20+
}
21+
{{- end }}
22+
23+
{{- if eq .Env.STACK_PAGE_CACHE_BACKEND "memcached" }}
24+
location = /.stack-cache-fetch {
25+
internal;
26+
27+
set $memc_key $args;
28+
29+
memc_connect_timeout 500ms;
30+
memc_read_timeout 500ms;
31+
memc_ignore_client_abort on;
32+
33+
memc_pass {{ default "localhost:11211" .Env.STACK_PAGE_CACHE_MEMCACHED_HOST_PATH }};
34+
}
35+
36+
37+
location = /.stack-cache-store {
38+
internal;
39+
40+
set_unescape_uri $exptime $arg_exptime;
41+
set_unescape_uri $key $arg_key;
42+
43+
set $memc_key $key;
44+
set $memc_exptime $exptime;
45+
46+
memc_connect_timeout 500ms;
47+
memc_send_timeout 500ms;
48+
memc_ignore_client_abort on;
49+
50+
memc_pass {{ default "localhost:11211" .Env.STACK_PAGE_CACHE_MEMCACHED_HOST_PATH }};
51+
}
52+
{{- end }}

php/docker/templates/nginx-vhost-conf.d/80-index.conf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,46 @@ location / {
55
}
66

77
location ~ \.php$ {
8+
{{- if .Env.STACK_PAGE_CACHE_BACKEND }}
9+
set $skip_cache 0;
10+
11+
# POST requests and urls with a query string should always go to PHP
12+
if ($request_method = POST) {
13+
set $skip_cache 1;
14+
}
15+
if ($query_string != "") {
16+
set $skip_cache 1;
17+
}
18+
19+
# Don't cache uris containing the following segments
20+
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
21+
set $skip_cache 1;
22+
}
23+
24+
# Don't use the cache for logged in users or recent commenters
25+
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
26+
set $skip_cache 1;
27+
}
28+
29+
set $key {{ default "nginx-cache:https$request_method$host$request_uri" .Env.STACK_PAGE_CACHE_KEY_PATTERN }};
30+
try_files $uri =404;
31+
32+
srcache_fetch_skip $skip_cache;
33+
srcache_store_skip $skip_cache;
34+
srcache_store_statuses {{ default "200 301 302" .Env.STACK_PAGE_CACHE_STORE_STATUSES }};
35+
36+
# https://github.com/openresty/srcache-nginx-module#srcache_response_cache_control
37+
srcache_response_cache_control off;
38+
39+
set_escape_uri $escaped_key $key;
40+
41+
srcache_fetch GET /.stack-cache-fetch $key;
42+
srcache_store PUT /.stack-cache-store key=$escaped_key&exptime={{ default "360" .Env.STACK_PAGE_CACHE_EXPIRE_SECONDS | atoi }};
43+
44+
more_set_headers 'x-cache-fetch $srcache_fetch_status';
45+
more_set_headers 'x-cache-store $srcache_store_status';
46+
{{- end }}
47+
848
fastcgi_pass $upstream;
949
fastcgi_read_timeout {{ max 60 (add 10 (default "30" .Env.PHP_REQUEST_TIMEOUT | atoi)) }};
1050
fastcgi_index index.php;

0 commit comments

Comments
 (0)