Skip to content

Commit

Permalink
Add support for page cache using Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanpetrea committed Jun 17, 2020
1 parent 87a009e commit d0831c9
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions php/docker/templates/nginx-vhost-conf.d/80-index.conf
Original file line number Diff line number Diff line change
@@ -1,12 +1,73 @@
# vim: set ft=nginx:

{{- $truthStrings := list "true" "on" "yes" }}

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
{{ if (has (default "off" .Env.PAGE_CACHE | lower) $truthStrings) }}
set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}

# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}

set $key "nginx-cache:https$request_method$host$request_uri";
try_files $uri =404;

srcache_fetch_skip $skip_cache;
srcache_store_skip $skip_cache;

# https://github.com/openresty/srcache-nginx-module#srcache_response_cache_control
srcache_response_cache_control off;

set_escape_uri $escaped_key $key;

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

more_set_headers 'x-cache-fetch $srcache_fetch_status';
more_set_headers 'x-cache-store $srcache_store_status';
{{ end }}

fastcgi_pass $upstream;
fastcgi_read_timeout {{ max 60 (add 10 (default "30" .Env.PHP_REQUEST_TIMEOUT | atoi)) }};
fastcgi_index index.php;
include /usr/local/openresty/nginx/conf/fastcgi.conf;
}

{{ if (has (default "off" .Env.PAGE_CACHE | lower) $truthStrings) }}
location /redis-fetch {
internal;

set $redis_key $args;
redis_pass {{ default "localhost:6379" .Env.PAGE_CACHE_REDIS_HOST_PATH }};
}

location /redis-store {
internal;

set_unescape_uri $exptime $arg_exptime;
set_unescape_uri $key $arg_key;

redis2_query set $key $echo_request_body;
redis2_query expire $key $exptime;
redis2_pass {{ default "localhost:6379" .Env.PAGE_CACHE_REDIS_HOST_PATH }};
}
{{ end }}

0 comments on commit d0831c9

Please sign in to comment.