Skip to content

Instantly share code, notes, and snippets.

@lloiacono
Created May 11, 2015 11:12
Show Gist options
  • Save lloiacono/fb286ae7c9b96879c8d9 to your computer and use it in GitHub Desktop.
Save lloiacono/fb286ae7c9b96879c8d9 to your computer and use it in GitHub Desktop.
Nginx Lua Redis rewrites
lua_package_path "/home/vagrant/ngx_openresty-1.7.10.1/bundle/lua-resty-redis-0.20/lib/?.lua;;";
server {
listen 80;
root /vagrant/test;
index index.html index.htm;
server_name leandro.test;
location / {
include /vagrant/rewrites.lua;
}
}
access_by_lua '
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local uri = string.gsub(ngx.var.request_uri, "?.*", "")
local params = string.gsub(ngx.var.request_uri, uri, "")
local res, err = red:get(uri)
if not res then
ngx.req.set_uri(ngx.var.request_uri)
return
end
if res == ngx.null then
ngx.req.set_uri(ngx.var.request_uri)
return
end
if params~=nil then
ngx.req.set_uri_args(params)
end
ngx.req.set_uri("/" .. res)
';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment