upstream redisbackend
{
server 192.168.50.1:6379;
keepalive 1024;
}
server
{
listen 9000 ;
server_name test;
#
# Cache settings
#
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
proxy_connect_timeout 60;
proxy_send_timeout 120;
proxy_read_timeout 180;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /tmp;
proxy_cache_use_stale updating;
proxy_set_header Accept-Encoding "";
proxy_ignore_headers Cache-Control;
more_clear_headers Server X-Powered-By;
#
# Logging
#
access_log /mnt/logs/openresty/test-access.log access;
error_log /mnt/logs/openresty/test-error.log error;
#
# Redis Read
#
location = /__redis_read
{
internal;
content_by_lua '
local key = ngx.md5(ngx.var.arg_key)
local redis = require "resty.redis"
local instance = redis:new()
instance:set_timeout(1000)
local ok, err = instance:connect("192.168.50.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local res, err = instance:get(key)
if not res then
ngx.say("failed to get cache: ", err)
return
end
if res == ngx.null then
ngx.say("failed to get cache: ")
return
end
ngx.print(res)
';
}
#
# Redis Write
#
location = /__redis_write
{
internal;
set_unescape_uri $ttl $arg_ttl;
set_unescape_uri $key $arg_key;
set_md5 $key;
#redis2_query auth "password"
#redis2_query select 1;
redis2_query set $key $echo_request_body;
redis2_query expire $key $ttl;
redis2_pass redisbackend;
}
#
# Catch all
#
location /
{
# Source
proxy_pass http://test:8080/;
# Redis
set $key $uri?$args;
set_escape_uri $escaped_key $key;
srcache_fetch GET /__redis_read key=$escaped_key;
srcache_store PUT /__redis_write key=$escaped_key&ttl=86400;
# No Cookies, Sir
proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;
# Adjust headers
add_header X-Cache-Status $srcache_fetch_status;
add_header X-Cache-Server $server_addr;
# Default Document
index index.hml;
# Rewrite to root
rewrite ^/$ /index.html permanent;
rewrite ^$ /index.html permanent;
}
}