|
1 | 1 | # vim: set ft=nginx:
|
2 | 2 |
|
| 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 | + |
3 | 8 | location / {
|
4 | 9 | try_files $uri $uri/ /index.php$is_args$args;
|
5 | 10 | }
|
6 | 11 |
|
7 | 12 | 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 | + |
8 | 112 | fastcgi_pass $upstream;
|
9 | 113 | fastcgi_read_timeout {{ max 60 (add 10 (default "30" .Env.PHP_REQUEST_TIMEOUT | atoi)) }};
|
10 | 114 | fastcgi_index index.php;
|
|
0 commit comments