昨天写了个地址重写的 Kong 自定义插件,跟大家分享下,话不多说,上代码
schema.lua
return {
no_consumer = true,
fields = {
regex = { type = "string" },
replacement = { type = "string" },
flag = {type = "string"},
},
self_check = function(schema, plugin_t, dao, is_update)
return true
end
}
handler.lua
local BasePlugin = require "kong.plugins.base_plugin"
local json = require("cjson")
local RewriteHandler = BasePlugin:extend()
local ngx = ngx
local kong = kong
RewriteHandler.PRIORITY = 2000
RewriteHandler.VERSION = "0.1.0"
function RewriteHandler:access(conf)
RewriteHandler.super.access(self)
local host = ngx.var.host
kong.log("Debug http-rewrite plugin, host is: ", host, " ,uri is: ", ngx.var.request_uri, " service is: ",service," ,config is: ", json.encode(conf))
kong.log("Debug http-rewrite plugin, regex is: ",regex, " replacement is : ",replacement)
local replace,n,err = ngx.re.sub(ngx.var.request_uri, conf.regex, conf.replacement)
if replace and n == 0 then
return
end
if err then
kong.log("Error http-rewrite plugin, ngx.re.sub err: ",err, " ,host is: ", host, " ,uri is: ",
ngx.var.request_uri, " ,config is: ", json.encode(conf))
return
end
kong.log("Debug http-rewrite plugin, replace is: ",replace)
if conf.flag == "redirect" then
ngx.redirect(replace,302)
elseif conf.flag == "permanent" then
ngx.redirect(replace,301)
end
end
function RewriteHandler:new()
RewriteHandler.super.new(self, "http-rewrite")
end
return RewriteHandler
效果如下:
开启该插件后,访问 http://dev.lyafei.com:8000/api/appmarket/app/appmarket/1.2/?service=/checkapp
被自动重定向到 http://dev.lyafei.com:8000/api/appstore/app/appmarket/1.2/?service=/checkapp
此处评论已关闭