--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Can you paste your code and minimal config to show a reproducible example of your problem?
location / {
root html;
index index.html index.htm;
set $replaceContent '';
content_by_lua_file /home/cxadmin/work/fromMemcached.lua;
body_filter_by_lua_file /home/cxadmin/work/fromUrlReplace.lua;
}
-- Load Memcached module
local memcached = require("resty.memcached")
local memcachedKey = ngx.var.host
-- Instantiate Memcached object
local memc, err = memcached:new()
if not memc then
ngx.log(ngx.ERR, "Failed to instantiate memc")
else
ngx.log(ngx.ERR, "memc successfully instantiated")
-- Set Memcached timeout
memc:set_timeout(1000)
-- Connect to Local Memcached
local ok, err = memc:connect("10.0.0.4", 11211)
if not ok then
ngx.log(ngx.ALERT, "Failed to connect to Local Memcached: ", err)
else
ngx.log(ngx.ERR, "Connected to local memcached")
-- Try to retrieve from Memcached
fromMemc, flags, err = memc:get(memcachedKey)
if err then
ngx.log(ngx.NOTICE, "Not found in Local Memcached: ", err)
end
if not fromMemc then
memc:close()
ngx.log(ngx.ERR, "memc closed")
return
end
ngx.log(ngx.NOTICE, "Value found and passed to $replaceContent", fromMemc)
ngx.var.replaceContent = fromMemc
memc:close()
ngx.log(ngx.ERR, "memc closed")
end
end
if not ngx.var.replaceContent then
ngx.log(ngx.NOTICE, "Retrieveing sript from URL")
-- Key will later be more complex
local http = require("socket.http")
local ltn12 = require("ltn12")
-- Request from URL
local t = {}
http.request {
url = "https://s3-us-west-1.amazonaws.com/cxeasylistcust/luatest.txt",
method = "GET",
sink = ltn12.sink.table(t)
}
-- Concatenete recieved chunks into string
dtext = table.concat(t)
replaceString = dtext .. "</head>"
else
ngx.log(ngx.NOTICE, "Using content from Memcached")
replaceString = ngx.var.replaceContent .. "</head>"
end
ngx.log(ngx.NOTICE, "replace string: ", replaceString)
ngx.arg[1] = ngx.re.sub(ngx.arg[1],"</head>", replaceString)
--
location / {
resolver 8.8.8.8;
root html;
index index.html index.htm;
content_by_lua_file '/home/cxadmin/Work/filter.lua';
replace_filter '</head>' 'ngx.ctx.replaceScript' ig;
}
local memcached = require("resty.memcached")
local memcachedKey = ngx.var.host
ngx.log(ngx.ALERT, "memcachedKey: ", memcachedKey)
local isScriptFromMemcached = false
memc, err = memcached:new()
-- Try to retrieve script from Memcached
if not memc then
ngx.log(ngx.ERR, "Failed to instantiate memc: ")
else
ngx.log(ngx.ALERT, "memc successfully instantiated")
memc:set_timeout(1000)
local ok, err = memc:connect("localhost", 11211)
if not ok then
ngx.log(ngx.ERR, "Failed to connect to Local Memcached: ", err)
else
ngx.log(ngx.ALERT, "Connected to local memcached, key: ", memcachedKey)
local scriptMemc, flags, err = memc:get(memcachedKey)
if err then
ngx.log(ngx.ALERT, "No appropriate script found in Local Memcached: ", err)
end
if scriptMemc then
ngx.log(ngx.ALERT, "Script found in memcached")
ngx.ctx.replaceScript = scriptMemc .. "</head>"
isScriptFromMemcached = true
else
ngx.log(ngx.ALERT, "Script not found in memcached")
end
end
end
-- If script wasn't found in Memcached - retrieve script directly from CX
if not isScriptFromMemcached then
ngx.log(ngx.ALERT, "Retrieveing sript from CX")
local http = require("socket.http")
local ltn12 = require("ltn12")
local t = {}
http.request {
url = "http://blabla.com/luatest.txt",
method = "GET",
sink = ltn12.sink.table(t)
}
-- Concatenete recieved chunks into string
scriptCX = table.concat(t)
ngx.log(ngx.ALERT, "Script from CX: ", scriptCX)
ngx.ctx.replaceScript = scriptCX .. "</head>"
ngx.log(ngx.ALERT, "memcachedKey: ", memcachedKey)
ngx.log(ngx.ALERT, "scrip: ", scriptCX)
-- local ok, err = memc:set(memcachedKey, scrip, 900)
if not err then ngx.log(ngx.ALERT, "Script stored in Memcached, ok: ", ok)
else ngx.log(ngx.ERR, "Failed to store the script in Memcached, err: ", err)
end
end
memc:close()
ngx.log(ngx.ALERT, "memc closed")
ngx.say(ngx.ctx.replaceScript)
--
You received this message because you are subscribed to a topic in the Google Groups "openresty-en" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openresty-en/bZi5GgqiuLs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openresty-en...@googlegroups.com.