#--------------------------------------
location / {
set $upstream "";
rewrite_by_lua '
local args = ngx.req.get_uri_args()
for key, val in pairs(args) do
if key == "token" then
ngx.log(ngx.ALERT, "Token IS :".. val)
-- load global route cache into current request scope
-- by default vars are not shared between requests
local routes = _G.routes
-- setup routes cache if empty
if routes == nil then
routes = {}
ngx.log(ngx.ALERT, "Route cache is empty.")
end
-- try cached route first
local route = routes[ngx.var.http_host]
if route == nil then
local redis = require "resty.redis"
local client = redis.connect("localhost", 6379)
route = client:get(val)
end
-- fallback to redis for lookups
if route ~= nil then
ngx.var.upstream = route
routes[ngx.var.http_host] = route
_G.routes = routes
else
ngx.exit(ngx.HTTP_NOT_FOUND)
break
end
end
end
';
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_connect_timeout 10;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://$upstream;
}
#---------------------------------------------
2017/10/13 20:48:34 [alert] 389#389: *1 [lua] rewrite_by_lua(nginx.conf:79):5: Token IS :mytoken01, client: 192.168.100.10, server: localhost, request: "GET /?token=mytoken01 HTTP/1.1", host: "web01.internal:89"
2017/10/13 20:48:34 [alert] 389#389: *1 [lua] rewrite_by_lua(nginx.conf:79):13: Route cache is empty., client: 192.168.100.10, server: localhost, request: "GET /?token=mytoken01 HTTP/1.1", host: "web01.internal:89"
2017/10/13 20:48:34 [error] 389#389: *1 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/redis.lua:78: bad argument #1 to 'rawget' (table expected, got string)
stack traceback:
coroutine 0:
[C]: in function 'rawget'
/usr/local/openresty/lualib/resty/redis.lua:78: in function 'connect'
rewrite_by_lua(nginx.conf:79):20: in function <rewrite_by_lua(nginx.conf:79):1>, client: 192.168.100.10, server: localhost, request: "GET /?token=mytoken01 HTTP/1.1", host: "web01.internal:89"
Regards,
Yichun