Custom Kong Plugin To Redirect Requests

I’m developing a custom plugin to redirect every request to a defined upstream service, the upstream service will then send the request back to the original service using the headers the plugin inserts to the request, after completing some DLP operations.

This is the schema.lua:

local typedefs = require "kong.db.schema.typedefs"

local PLUGIN_NAME = "myplugin"

local schema = {
  name = PLUGIN_NAME,
  fields = {
      config = {
        type = "record",
        fields = {
          { proxy_host = { required = true, type = "string" } },
          { proxy_port = { required = true, type = "number" } }

return schema

This is the handler.lua:

local plugin_name = "forward-proxy"
local url = require "net.url"

function dump(o)
  if type(o) == 'table' then
     local s = '{ '
     for k,v in pairs(o) do
        if type(k) ~= 'number' then k = '"'..k..'"' end
        s = s .. '['..k..'] = ' .. dump(v) .. ','
     return s .. '} '
     return tostring(o)

local plugin = {
  PRIORITY = 999,
  VERSION = "0.1",

function plugin:access(plugin_conf)

  local origian_service = kong.router.get_service()

  kong.log.debug("WALLAH GIRMISEM PLUGINE    ",, "-----", origian_service.port, "------", origian_service.path)
  -- Set original host, port, and path as headers
  ngx.req.set_header("X-Original-Port", origian_service.port or "80")
  ngx.req.set_header("X-Original-Path", origian_service.path or "/")

  kong.log.debug("GELECEK SERVISLER SIZLERE EMANETTIR    ", plugin_conf.proxy_host, "-----", plugin_conf.proxy_port)

  -- Use kong.service.set_upstream to change the upstream target
  kong.service.set_upstream(plugin_conf.proxy_host, plugin_conf.proxy_port)

return plugin

The plugin inserts the headers as expected however
kong.service.set_upstream(plugin_conf.proxy_host, plugin_conf.proxy_port)
line doesn’t seem to direct the request to the plugin_conf.proxy_host as I expected. The request gets sent to the original upstream service. Is there an easy way to achieve this functionality


I have managed to send some requests to the simple flask service running on my local machine by changing kong.service.set_upstream(plugin_conf.proxy_host, plugin_conf.proxy_port)
to kong.service.set_target("host.docker.internal", 5000) . Now the flask service recieves to request but can not parse it, it gives the following error: - - [18/Dec/2023 23:10:06] code 400, message Bad request syntax (“\x16\x03\x01\x00Ô\x01\x00\x00Ð\x03\x03\x91\x00)ÐÞ\x94ë:ù&úµl\x9eù\x15Îñ\x9e³È.a^ô{:ýf4¥6\x00\x008À,À0\x00\x9f̨̩̪À+À/\x00\x9eÀ$À(\x00kÀ#À’\x00gÀ”)

The service itself is super simple so the problem can not be there:

app = Flask(__name__)

from flask import Flask, request

@app.route('/', methods=['GET', 'POST'])
def print_request():
    request_data =
    print(f"Received POST request data: {request_data}")
    return "POST request received and printed.\n"

if __name__ == '__main__':'', port=5000)

I am using pongo with the plugin development template to develop this plugin, the kong version used is 3.4.2