|
1 | 1 | # vim: set ft=nginx:
|
2 | 2 |
|
| 3 | +{{- $truthStrings := list "true" "on" "yes" }} |
| 4 | + |
3 | 5 | location / {
|
4 | 6 | try_files $uri $uri/ /index.php$is_args$args;
|
5 | 7 | }
|
6 | 8 |
|
7 | 9 | 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 | + |
8 | 50 | fastcgi_pass $upstream;
|
9 | 51 | fastcgi_read_timeout {{ max 60 (add 10 (default "30" .Env.PHP_REQUEST_TIMEOUT | atoi)) }};
|
10 | 52 | fastcgi_index index.php;
|
11 | 53 | include /usr/local/openresty/nginx/conf/fastcgi.conf;
|
12 | 54 | }
|
| 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