Skip to content

Commit

Permalink
enable caching behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
leandromoreira committed Dec 23, 2021
1 parent 5af21fe commit 1d213f9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:
- "./generic_conf/basic_vts_location.conf:/usr/local/openresty/nginx/conf/basic_vts_location.conf"
- "./generic_conf/basic_vts_setup.conf:/usr/local/openresty/nginx/conf/basic_vts_setup.conf"
- "./generic_conf/lua_path_setup.conf:/usr/local/openresty/nginx/conf/lua_path_setup.conf"
- "./generic_conf/setup_cache.conf:/usr/local/openresty/nginx/conf/setup_cache.conf"
- "./generic_conf/define_cache.conf:/usr/local/openresty/nginx/conf/define_cache.conf"
- "./src/:/lua/src/"

backend:
Expand Down
21 changes: 21 additions & 0 deletions generic_conf/backend_definition.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
env BACKEND_HOST; # allow list for os.getenv
env BACKEND_PORT; # allow list for os.getenv

upstream backend {
server 0.0.0.1; # just an invalid address as a place holder

balancer_by_lua_block {
local balancer = require "ngx.balancer"
local host = os.getenv("BACKEND_HOST")
local port = number(os.getenv("BACKEND_PORT"))

local ok, err = balancer.set_current_peer(host, port)
if not ok then
ngx.log(ngx.ERR, "failed to set the current peer: ", err)
return ngx.exit(500)
end
}

keepalive 10; # connection pool
}

8 changes: 8 additions & 0 deletions generic_conf/define_cache.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
proxy_cache zone_1;
proxy_cache_key $cache_key;
proxy_cache_lock on;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering on;
proxy_buffers 16 16k;
add_header X-Cache-Status $upstream_cache_status;
6 changes: 6 additions & 0 deletions generic_conf/setup_cache.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
proxy_cache_path /cache/ levels=2:2 keys_zone=zone_1:10m max_size=10m inactive=10m use_temp_path=off;
proxy_cache_lock_timeout 1s;
proxy_cache_use_stale error timeout updating;
proxy_read_timeout 1s;
proxy_send_timeout 1s;
proxy_ignore_client_abort on;
7 changes: 6 additions & 1 deletion nginx_edge.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ http {

include lua_path_setup.conf;
include basic_vts_setup.conf;
include setup_cache.conf;

upstream backend {
server backend:8080;
Expand All @@ -19,8 +20,12 @@ http {
listen 8080;

location / {
set_by_lua_block $cache_key {
return ngx.var.uri
}

proxy_pass http://backend;
add_header X-Cache-Status $upstream_cache_status;
include define_cache.conf;
add_header X-Edge Server;
}

Expand Down

0 comments on commit 1d213f9

Please sign in to comment.