OpenResty as redirect server for logging sent to Kafka
Posted onEdited on
Purpose
A redirect server for logging is used to trace various urls user clicked. The redirect server will log any query parameters in url and redirect to url parameter finally. The logging should be doing asynchronously to minimize redirect time.
OpenResty is a combination of Nginx and Lua. I won’t explain that here, you could google it to know more.
lua_package_path "/usr/local/openresty/lualib/resty/kafka/?.lua;;"; # without resolver setting, it will fail in DNS lookup # either 8.8.8.8 or using container's resolver # resolver 8.8.8.8 resolver local=on ipv6=off; resolver_timeout 5s;
server { listen 8089; server_name localhost;
location / { root html; index index.html index.htm; }
# Make sure Lua is working location /hello { default_type text/html; content_by_lua ' ngx.say("Lua: hello world!") '; }
# The default 1by1 gif location /empty_gif { empty_gif; }
location /rd { default_type text/html; content_by_lua ' local cjson = require "cjson" local producer = require "resty.kafka.producer" -- local client = require "resty.kafka.client" local broker_list = { { host = "192.168.0.16", port = 9092}, { host = "192.168.0.17", port = 9092}, { host = "192.168.0.18", port = 9092} } -- Parsing request args local params_json = {} local args, err = ngx.req.get_uri_args() if err == "truncated" then -- one can choose to ignore or reject the current request here end for key, val in pairs(args) do params_json[key] = val end ngx.log(ngx.ERR, "url: ", params_json["d"]) local topic = "redirect-server" local params_message = cjson.encode(params_json) -- Set producer async local bp = producer:new(broker_list, { producer_type = "async" }) -- The second parameter of send method is to control kafka routing. -- When it is nill, it will write message to same partition -- With designated key,it will write message to hash of key of partition local ok, err = bp:send(topic, nil, params_message) -- For debugging ngx.log(ngx.ERR, "Message in json: ", params_message) if not ok then ngx.log(ngx.ERR, "kafka send err:", err) return end if params_json["url"] ~= nil then return ngx.redirect(params_json["url"], 301) end return ngx.exec("/empty_gif") ';
} } }
Testing
Check you logs/error.log file, there should not have any kafka send err: