Git Product home page Git Product logo

Comments (20)

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024 2

Kong is not designed to work with internal redirects, so the usage is not recommended. Would you share the use case so we can help you find an alternative solution?

our tech team plans to replace nginx with kong. In the previous nginx, internal redirection was configured for short link services. The purpose of using internal redirection is to protect the real resource path of the backend, can Kong support this internal redirects feature, otherwise we won't be able to switch from nginx to Kong

This can be achieved in more than 1 way.

  1. If you do not need redirected requests to go through a different set of plugins, you could use kong.service.set_target;
  2. Or you could use a reverse proxy. Either with another Nginx or a separate route of the same Kong instance should work;
  3. Or you could customize the rewrite phase handler, to rewrite the URL before Kong's router can see it. This can be done with a global plugin or a custom Nginx template.

from kong.

ADD-SP avatar ADD-SP commented on May 26, 2024

@chronolaw Could you please take a look as I'm not familiar with router.

from kong.

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024

Kong is not designed to work with internal redirects, so the usage is not recommended.
Would you share the use case so we can help you find an alternative solution?

from kong.

kanbo avatar kanbo commented on May 26, 2024

Kong is not designed to work with internal redirects, so the usage is not recommended. Would you share the use case so we can help you find an alternative solution?

our tech team plans to replace nginx with kong. In the previous nginx, internal redirection was configured for short link services. The purpose of using internal redirection is to protect the real resource path of the backend, can Kong support this internal redirects feature, otherwise we won't be able to switch from nginx to Kong

from kong.

kanbo avatar kanbo commented on May 26, 2024

Kong is not designed to work with internal redirects, so the usage is not recommended. Would you share the use case so we can help you find an alternative solution?

our tech team plans to replace nginx with kong. In the previous nginx, internal redirection was configured for short link services. The purpose of using internal redirection is to protect the real resource path of the backend, can Kong support this internal redirects feature, otherwise we won't be able to switch from nginx to Kong

This can be achieved in more than 1 way.

  1. If you do not need redirected requests to go through a different set of plugins, you could use kong.service.set_target;
  2. Or you could use a reverse proxy. Either with another Nginx or a separate route of the same Kong instance should work;
  3. Or you could customize the rewrite phase handler, to rewrite the URL before Kong's router can see it. This can be done with a global plugin or a custom Nginx template.

my solution is similar to the third way,I modify the kong source code ,kong/router/atc.lua ,the problem solved, please help me check if the following solution is correct ???

function _M:exec(ctx)
local req_uri = ctx and ctx.request_uri or var.request_uri
if req_uri ~= var.uri then
req_uri = var.uri

end
... ...

from kong.

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024

Kong is not designed to work with internal redirects, so the usage is not recommended. Would you share the use case so we can help you find an alternative solution?

our tech team plans to replace nginx with kong. In the previous nginx, internal redirection was configured for short link services. The purpose of using internal redirection is to protect the real resource path of the backend, can Kong support this internal redirects feature, otherwise we won't be able to switch from nginx to Kong

This can be achieved in more than 1 way.

  1. If you do not need redirected requests to go through a different set of plugins, you could use kong.service.set_target;
  2. Or you could use a reverse proxy. Either with another Nginx or a separate route of the same Kong instance should work;
  3. Or you could customize the rewrite phase handler, to rewrite the URL before Kong's router can see it. This can be done with a global plugin or a custom Nginx template.

my solution is similar to the third way,I modify the kong source code ,kong/router/atc.lua ,the problem solved, please help me check if the following solution is correct ???

function _M:exec(ctx) local req_uri = ctx and ctx.request_uri or var.request_uri if req_uri ~= var.uri then req_uri = var.uri end ... ...

It looks like you are directly modifying ngx.var.uri. You should modify the URI with https://github.com/openresty/lua-nginx-module#ngxreqset_uri.
And this does not seem the right way. except for atc.lua, there's another flavor of router implemented with tradtional.lua.

from kong.

kanbo avatar kanbo commented on May 26, 2024

Kong is not designed to work with internal redirects, so the usage is not recommended. Would you share the use case so we can help you find an alternative solution?

our tech team plans to replace nginx with kong. In the previous nginx, internal redirection was configured for short link services. The purpose of using internal redirection is to protect the real resource path of the backend, can Kong support this internal redirects feature, otherwise we won't be able to switch from nginx to Kong

This can be achieved in more than 1 way.

  1. If you do not need redirected requests to go through a different set of plugins, you could use kong.service.set_target;
  2. Or you could use a reverse proxy. Either with another Nginx or a separate route of the same Kong instance should work;
  3. Or you could customize the rewrite phase handler, to rewrite the URL before Kong's router can see it. This can be done with a global plugin or a custom Nginx template.

my solution is similar to the third way,I modify the kong source code ,kong/router/atc.lua ,the problem solved, please help me check if the following solution is correct ???
function _M:exec(ctx) local req_uri = ctx and ctx.request_uri or var.request_uri if req_uri ~= var.uri then req_uri = var.uri end ... ...

It looks like you are directly modifying ngx.var.uri. You should modify the URI with https://github.com/openresty/lua-nginx-module#ngxreqset_uri. And this does not seem the right way. except for atc.lua, there's another flavor of router implemented with tradtional.lua.

I'm not directly modifying ngx.var.uri ,I just get the real internal redirection url (ngx.var.uri) and modify kong req_uri , refer to your suggestion, i will try to write a global plugin to modify kong req_uri , thank you.

from kong.

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024

Glad to know it helps. Closing the ticket. Please feel free to reopen if you have further concerns.

from kong.

kanbo avatar kanbo commented on May 26, 2024

Glad to know it helps. Closing the ticket. Please feel free to reopen if you have further concerns.

i need redirected requests to go through a different set of plugins ,so i try the third way you suggest,but it still cannot solve the problem

function InternalRedirectionHandler:rewrite(conf)
local uri = ngx.var.uri
local request_uri = ngx.var.request_uri
local path = string.match(request_uri, "^([^?]+)")
if path and path ~= uri then
ngx.req.set_uri(uri)
end
end

because Kong's router read ngx.var.request_uri, we cannot modified it by ngx.req.set_uri(), it is read-only,

Kong's router(kong/router/atc.lua) can read ngx.var.uri(when kong redirect occurs ,ngx.var.uri is the right path) ,not ngx.var.request_uri?

from kong.

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024

@kanbo The 3rd method I proposed is kind of a hack and sorry that does not work. I guess we need to take the 2nd method.

from kong.

kanbo avatar kanbo commented on May 26, 2024

@kanbo The 3rd method I proposed is kind of a hack and sorry that does not work. I guess we need to take the 2nd method.

This way requires additional deployment, which is not what we want

from kong.

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024

@kanbo The 3rd method I proposed is kind of a hack and sorry that does not work. I guess we need to take the 2nd method.

This way requires additional deployment, which is not what we want

Maybe have a try with another route with the same Kong instance?

from kong.

kanbo avatar kanbo commented on May 26, 2024

@kanbo The 3rd method I proposed is kind of a hack and sorry that does not work. I guess we need to take the 2nd method.

This way requires additional deployment, which is not what we want

Maybe have a try with another route with the same Kong instance?

another route ?, i do not know how to config and test , please explain in detail

from kong.

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024

@kanbo Add a route to Kong, with its upstream set to the redirection target, so basically use Kong itself as that Nginx to do the redirection.

from kong.

kanbo avatar kanbo commented on May 26, 2024

@kanbo Add a route to Kong, with its upstream set to the redirection target, so basically use Kong itself as that Nginx to do the redirection.
so the complete request order is: nginx ->target(occur redirect) ->kong ->redirect target ?

from kong.

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024

@kanbo Add a route to Kong, with its upstream set to the redirection target, so basically use Kong itself as that Nginx to do the redirection.
so the complete request order is: nginx ->target(occur redirect) ->kong ->redirect target ?

kong->kong->redirect target

from kong.

kanbo avatar kanbo commented on May 26, 2024

@StarlightIbuki kong can not get real redirect path, it only read ngx.var.request_ uri, so i think this way(kong->kong->redirect target) doesn't work

from kong.

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024

@StarlightIbuki kong can not get real redirect path, it only read ngx.var.request_ uri, so i think this way(kong->kong->redirect target) doesn't work

What I mean is to set the upstream of a route to another, say you're deploying the site on port 80:
route1: /to_be_redirected - > localhost:80/redirect_target_path
route2: /redirect_target_path->the real upstream

If we want to redirect dynamically, we can write a custom plugin for route1 to set the target

from kong.

kanbo avatar kanbo commented on May 26, 2024

@StarlightIbuki kong can not get real redirect path, it only read ngx.var.request_ uri, so i think this way(kong->kong->redirect target) doesn't work

What I mean is to set the upstream of a route to another, say you're deploying the site on port 80: route1: /to_be_redirected - > localhost:80/redirect_target_path route2: /redirect_target_path->the real upstream

If we want to redirect dynamically, we can write a custom plugin for route1 to set the target
this is the first method you suggested (kong.service.set_target) , but i need redirected requests to go through a different set of plugins

from kong.

StarlightIbuki avatar StarlightIbuki commented on May 26, 2024

@StarlightIbuki kong can not get real redirect path, it only read ngx.var.request_ uri, so i think this way(kong->kong->redirect target) doesn't work

What I mean is to set the upstream of a route to another, say you're deploying the site on port 80: route1: /to_be_redirected - > localhost:80/redirect_target_path route2: /redirect_target_path->the real upstream
If we want to redirect dynamically, we can write a custom plugin for route1 to set the target
this is the first method you suggested (kong.service.set_target) , but i need redirected requests to go through a different set of plugins

It's slightly different. The request will then be handled by Kong again, and thus it will go through the plugin set bound to the second route.

from kong.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.