Skip to content

Commit 96e316c

Browse files
committed
Add support for page cache using Redis
1 parent 87a009e commit 96e316c

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,74 @@
11
# vim: set ft=nginx:
22

3+
{{- $truthStrings := list "true" "on" "yes" }}
4+
35
location / {
46
try_files $uri $uri/ /index.php$is_args$args;
57
}
68

79
location ~ \.php$ {
10+
{{ if (has (default "off" .Env.PAGE_CACHE | lower) $truthStrings) }}
11+
set $skip_cache 0;
12+
13+
# POST requests and urls with a query string should always go to PHP
14+
if ($request_method = POST) {
15+
set $skip_cache 1;
16+
}
17+
if ($query_string != "") {
18+
set $skip_cache 1;
19+
}
20+
21+
# Don't cache uris containing the following segments
22+
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
23+
set $skip_cache 1;
24+
}
25+
26+
# Don't use the cache for logged in users or recent commenters
27+
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
28+
set $skip_cache 1;
29+
}
30+
31+
set $key "nginx-cache:https$request_method$host$request_uri";
32+
try_files $uri =404;
33+
34+
add_header X-my-header my-header-content;
35+
36+
srcache_fetch_skip $skip_cache;
37+
srcache_store_skip $skip_cache;
38+
39+
srcache_response_cache_control off;
40+
41+
set_escape_uri $escaped_key $key;
42+
43+
srcache_fetch GET /redis-fetch $key;
44+
srcache_store PUT /redis-store key=$escaped_key&exptime=120;
45+
46+
more_set_headers 'X-Cache $srcache_fetch_status';
47+
more_set_headers 'X-Cache-2 $srcache_store_status';
48+
{{ end }}
49+
850
fastcgi_pass $upstream;
951
fastcgi_read_timeout {{ max 60 (add 10 (default "30" .Env.PHP_REQUEST_TIMEOUT | atoi)) }};
1052
fastcgi_index index.php;
1153
include /usr/local/openresty/nginx/conf/fastcgi.conf;
1254
}
55+
56+
{{ if (has (default "off" .Env.PAGE_CACHE | lower) $truthStrings) }}
57+
location /redis-fetch {
58+
internal;
59+
60+
set $redis_key $args;
61+
redis_pass {{ default "localhost:6379" .Env.PAGE_CACHE_REDIS_HOST_PATH }};
62+
}
63+
64+
location /redis-store {
65+
internal;
66+
67+
set_unescape_uri $exptime $arg_exptime;
68+
set_unescape_uri $key $arg_key;
69+
70+
redis2_query set $key $echo_request_body;
71+
redis2_query expire $key $exptime;
72+
redis2_pass {{ default "localhost:6379" .Env.PAGE_CACHE_REDIS_HOST_PATH }};
73+
}
74+
{{ end }}

0 commit comments

Comments
 (0)