-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use an optional shared Redis cache for multiple NGINX instances
* docker compose example development configuration * Redis cache documentation
- Loading branch information
Showing
12 changed files
with
203 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
*.properties | ||
*.crt | ||
*.key | ||
*.lua | ||
.*ignore | ||
.husky | ||
.gitattributes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
map "" $cache_status { | ||
default "BYPASS"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
proxy_cache_path /tmp/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; | ||
proxy_cache my_cache; | ||
proxy_cache_use_stale error timeout http_404 http_500 http_502 http_503 http_504; | ||
|
||
map "$upstream_cache_status:$srcache_fetch_status" $cache_status { | ||
":BYPASS" "BYPASS"; | ||
"~HIT" "HIT"; | ||
"~^MISS" "MISS"; | ||
default "-"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
local function extract_ssl(uri) | ||
if string.sub(uri,1,9) == "rediss://" then | ||
return true, string.gsub(uri, "rediss://", "redis://") | ||
else | ||
return false, uri | ||
end | ||
end | ||
|
||
local function connect() | ||
local uri = os.getenv("REDIS_URI") | ||
if not uri then | ||
ngx.log(ngx.ERR, "REDIS_URI not set") | ||
ngx.exit(500) | ||
end | ||
|
||
local ssl, uri = extract_ssl(uri) | ||
|
||
local redis, err = require("resty.redis.connector").new({ | ||
url = uri, | ||
connection_options = { | ||
ssl = ssl | ||
} | ||
}):connect() | ||
|
||
if err ~= nil then | ||
ngx.log(ngx.ERR, "Failed to connect to redis, error -> ", err) | ||
ngx.exit(500) | ||
end | ||
|
||
return redis | ||
end | ||
|
||
return { | ||
connect = connect | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{{- if and (getenv "CACHE" | strings.ToLower | regexp.Match "on|1|true|yes") (getenv "REDIS_URI") -}} | ||
location /redis-fetch { | ||
internal; | ||
|
||
resolver local=on ipv6=off; | ||
resolver_timeout 5s; | ||
|
||
content_by_lua_block { | ||
local key = assert(ngx.var.arg_key, "no key found") | ||
|
||
local red = require("redis-integration").connect() | ||
local data, err = red:get(key) | ||
assert(red:set_keepalive(10000, 100)) | ||
if err ~= nil then | ||
ngx.log(ngx.ERR, "Failed to get data from redis, error -> ", err) | ||
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) | ||
elseif data == ngx.null then | ||
ngx.exit(ngx.HTTP_NOT_FOUND) | ||
else | ||
ngx.print(data) | ||
ngx.exit(ngx.HTTP_OK) | ||
end | ||
} | ||
} | ||
|
||
location /redis-store { | ||
internal; | ||
|
||
resolver local=on ipv6=off; | ||
resolver_timeout 5s; | ||
|
||
content_by_lua_block { | ||
local value = assert(ngx.req.get_body_data(), "no value found") | ||
local key = assert(ngx.var.arg_key, "no key found") | ||
local exptime = assert(ngx.var.arg_exptime, "no exptime found") | ||
|
||
local red = require("redis-integration").connect() | ||
local ok, err = red:set(key, value) | ||
if not ok then | ||
ngx.log(ngx.ERR, "Failed to set data to redis, error -> ", err) | ||
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) | ||
else | ||
assert(red:expire(key, exptime)) | ||
assert(red:set_keepalive(10000, 100)) | ||
ngx.exit(ngx.HTTP_OK) | ||
end | ||
} | ||
} | ||
{{- end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{{- if getenv "CACHE" | strings.ToLower | regexp.Match "on|1|true|yes" -}} | ||
{{- if getenv "REDIS_URI" -}} | ||
|
||
srcache_response_cache_control off; | ||
srcache_store_statuses 200 302 404; | ||
|
||
srcache_default_expire {{ getenv "CACHE_DURATION_NGINX_OK" }}; | ||
|
||
# make a nice cache key without parameters | ||
set_sha1 $key $scheme://$host$uri$c_uri; | ||
|
||
srcache_fetch GET /redis-fetch key=$key; | ||
srcache_store PUT /redis-store key=$key&exptime=$srcache_expire; | ||
{{- else -}} | ||
proxy_ignore_headers Cache-Control; | ||
proxy_cache_valid 200 302 {{ getenv "CACHE_DURATION_NGINX_OK" }}; | ||
proxy_cache_valid 404 {{ getenv "CACHE_DURATION_NGINX_NF" }}; | ||
|
||
# make a nice cache key without parameters | ||
proxy_cache_key $scheme://$host$uri$c_uri; | ||
{{- end -}} | ||
|
||
add_header X-Cache-Status $cache_status; | ||
{{- end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters