Skip to content

Commit bd19bf6

Browse files
committed
Add support for page cache
1 parent 87a009e commit bd19bf6

File tree

3 files changed

+155
-0
lines changed

3 files changed

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

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,114 @@
11
# vim: set ft=nginx:
22

3+
{{- $stackBackends := list "redis" "memcached" "custom" }}
4+
{{- $truthStrings := list "true" "on" "yes" -}}
5+
{{- $keyPrefix := default "nginx-cache:" .Env.STACK_PAGE_CACHE_KEY_PREFIX }}
6+
{{- $keyUID := default "https$request_method$host$request_uri" .Env.STACK_PAGE_CACHE_KEY_UID }}
7+
38
location / {
49
try_files $uri $uri/ /index.php$is_args$args;
510
}
611

712
location ~ \.php$ {
13+
{{- if has .Env.STACK_PAGE_CACHE_BACKEND $stackBackends }}
14+
set $skip_cache 0;
15+
16+
# POST requests and urls with a query string should always go to PHP
17+
if ($request_method = POST) {
18+
set $skip_cache 1;
19+
}
20+
if ($query_string != "") {
21+
set $skip_cache 1;
22+
}
23+
24+
# Don't cache uris containing the following segments
25+
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
26+
set $skip_cache 1;
27+
}
28+
29+
# Don't use the cache for logged in users or recent commenters
30+
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
31+
set $skip_cache 1;
32+
}
33+
34+
set $key "";
35+
36+
# Use salted keys with memcached by default, for implementing cache flush
37+
{{- if and (eq .Env.STACK_PAGE_CACHE_BACKEND "memcached") ( has (default "on" .Env.STACK_PAGE_CACHE_MEMCACHED_USE_SALT) $truthStrings) }}
38+
rewrite_by_lua '
39+
local memcached = require "resty.memcached"
40+
local memc, err = memcached:new()
41+
if not memc then
42+
ngx.log(ngx.ERR, "failed to instantiate memcached: ", err)
43+
return
44+
end
45+
46+
memc:set_timeout(1000)
47+
48+
local ok, err = memc:connect("{{ default "127.0.0.1:11211" .Env.STACK_PAGE_CACHE_MEMCACHED_HOST_PATH }}")
49+
if not ok then
50+
ngx.log(ngx.ERR, "failed to connect to memcached: ", err)
51+
return
52+
end
53+
54+
local keyPrefix = "{{ $keyPrefix }}"
55+
local saltKey = keyPrefix .. "salt"
56+
57+
local salt, flags, err = memc:get(saltKey)
58+
if err then
59+
ngx.log(ngx.ERR, "failed to get memcached salt: ", err)
60+
return
61+
end
62+
63+
if not salt then
64+
ngx.log(ngx.INFO, "memcached salt not found, attempting to set one...")
65+
66+
salt = os.time()
67+
local ok, err = memc:set(saltKey, salt)
68+
if not ok then
69+
ngx.log(ngx.ERR, "failed to set memcached salt: ", err)
70+
return
71+
end
72+
end
73+
74+
ngx.var.key = keyPrefix .. salt .. ":{{ $keyUID }}"
75+
76+
local ok, err = memc:close()
77+
if not ok then
78+
ngx.say("failed to close memcached: ", err)
79+
return
80+
end
81+
';
82+
83+
{{- else }}
84+
# default key is nginx-cache:https$request_method$host$request_uri
85+
set $key {{ $keyPrefix }}{{ $keyUID }};
86+
{{- end }}
87+
88+
if ($key = "") {
89+
set $skip_cache 1;
90+
}
91+
92+
try_files $uri =404;
93+
94+
srcache_fetch_skip $skip_cache;
95+
srcache_store_skip $skip_cache;
96+
srcache_store_statuses {{ default "200 301 302" .Env.STACK_PAGE_CACHE_STORE_STATUSES }};
97+
98+
# https://github.com/openresty/srcache-nginx-module#srcache_response_cache_control
99+
srcache_response_cache_control {{ default "on" .Env.STACK_PAGE_CACHE_RESPONSE_CACHE_CONTROL }};
100+
101+
set_escape_uri $escaped_key $key;
102+
103+
srcache_fetch GET /.stack-cache-fetch $key;
104+
srcache_store PUT /.stack-cache-store key=$escaped_key&exptime={{ default "360" .Env.STACK_PAGE_CACHE_EXPIRE_SECONDS | atoi }};
105+
106+
more_set_headers "x-cache-skip $skip_cache";
107+
more_set_headers "x-cache-key $key";
108+
more_set_headers "x-cache-fetch $srcache_fetch_status";
109+
more_set_headers "x-cache-store $srcache_store_status";
110+
{{- end }}
111+
8112
fastcgi_pass $upstream;
9113
fastcgi_read_timeout {{ max 60 (add 10 (default "30" .Env.PHP_REQUEST_TIMEOUT | atoi)) }};
10114
fastcgi_index index.php;

0 commit comments

Comments
 (0)