As Kong does not support routing requests based on ports, we have to use the pre-function plugin to realize it.
Please refer bellow procedure to send requests based on different ports
(Please modify Lua script/service object/route object depend on your requirements)
Prerequisite: The pre-function plugin’s kong.service.set_target() call requires the untrusted_lua Kong configuration property to be set to on or lax (the default is now strict, which excludes this call). Set KONG_UNTRUSTED_LUA=on and restart Kong before proceeding, otherwise the plugin will fail with a 500 error: attempt to call field 'set_target' (a nil value).
-
Enable multiple proxy ports for Kong and restart Kong.
Modify below parameter in kong configuration
KONG_PROXY_LISTEN/proxy_listen=0.0.0.0:7000, 0.0.0.0:8000, 0.0.0.0:9000, 0.0.0.0:8443 ssl
Restart Kong
-
Create below service object and route object
service:
name: test
url: https://<upstream1>:443/xxx
route:
name: test
path: /test
-
Write a Lua script like below, let’s name it as redirect.lua
local port = kong.request.get_port()
if port == 8000 then
kong.service.set_target("<upstream2>", 443)
kong.service.request.set_scheme("https")
kong.service.request.set_path("/xxx")
elseif port == 9000 then
kong.service.set_target("<upstream3>", 443)
kong.service.request.set_scheme("https")
kong.service.request.set_path("/xxx")
end
-
Enable the pre-function plugin on the route object we created in step 2
curl -X POST http://<kong>:8001/routes/test/plugins \
-F "name=pre-function" \
-F "config.access[1]=@/path/to/redirect.lua"
-
Testing
curl -i http://<kong>:7000/test
> response from upstream1
curl -i http://<kong>:8000/test
> response from upstream2
curl -i http://<kong>:9000/test
> response from upstream3
As we could see,
request from 7000 port has been proxied to upstream1,
request from 8000 port has been proxied to upstream2,
request from 9000 port has been proxied to upstream3.