Routes Aliasing

Requirement:
Service S1: http://localhost:8000

Registered Route R1: /api/books/1/stories
Redirect to http://localhost:8000/books/1/stories

Registered Route R2: /api/shelves/1/books
Redirect to http://localhost:8000/shelves/1/books

One solution could be that I register /api with strip_path = false, but I want to define rules using acl plugin at every route and if I only register one route(/api) then I cannot define different acl for R1 and R2. How can I do both?

1 Like

You should define your Services based on the upstream URLs you are pointing Kong to. In this case, I see not one, but two Services:

S1
url = http://localhost:8000/books/1/stories

S2
url = http://localhost:8000/shelves/1/books

In which case, you could then define the following Routes:

R1
paths = ["/api/books/1/stories"]
strip_url = true
service = S1

R2
paths = ["/api/shelves/1/books"]
strip_url = true
service = S2

As of Kong 0.14 (and 1.0), and until Kong supports injecting dynamic variables into a Service’s URL, this would be a viable way of splitting your definitions in two and applying different plugins to each Route/Service.

A more advanced option could also be to implement your own plugin, and define regex paths with capture groups to extract parts of the request’s URL, and make the custom plugin inject it into the upstream request. This is a more advanced and cumbersome technique, it depends on your actual use-case and how you see your Kong implementation evolving. I would recommend going the easy road for now.

By the way, make sure to change Kong’s default proxy listening port (which is 8000, so that it doesn’t try to bind on the same port and your running application.