Senior LevelREST APIs & Web Services
How do you write a custom High-Performance Lua Plugin for Kong API Gateway?
❓ Interview Prompt: Explain Kong API Gateway plugin architecture, OpenResty / NGINX Lua execution phase hooks (`access`, `header_filter`), and high-performance request transformation.
Extending Kong API Gateway capabilities using custom Lua plugins running on the OpenResty / NGINX event loop:
OpenResty Execution Phases:
•
'access()': Executed for every request before forwarding to upstream microservices. Ideal for custom HMAC validation or token transformation.
•
'header_filter()': Executed when receiving response headers from upstream. Ideal for stripping internal server headers.
•
Operates asynchronously with sub-millisecond overhead!
Code Implementation Example
lua
-- Kong Custom Access Plugin (custom_auth/handler.lua)
local CustomHandler = { PRIORITY = 1000, VERSION = "1.0.0" }
function CustomHandler:access(conf)
local token = kong.request.get_header("X-Custom-Auth")
if not token or token ~= conf.secret_token then
return kong.response.exit(401, { message = "Invalid Custom Authorization Header" })
end
end
return CustomHandler💡Custom Kong API Gateway Lua plugin executing in access phase.
🎯 Interview Key Takeaway
Write custom Kong plugins in Lua running on OpenResty execution phases to execute custom security checks with sub-millisecond latency.
#Kong#Lua#API Gateway#OpenResty#Senior
Back to all REST APIs & Web Services Questions→