Standardize requests to lowercase for proxy-cache to work? #7821
-
Hello, My issue I got is that people are sending requests to my API with most of the times lowercase letter, but some also send first letter as capital or randomized, since the proxy-cache handles the requests as unique. On this way, they can get around my cache, which is a bit annoying actually. Example paths:
Is there some smart thing that can make proxy-cache handle those requests as the same request? Maybe place an nginx in front of the Kong gateway to rewrite the path with lua? Maybe that it passes it to the backend but there I return a redirect to the lowercase url if I detect some uppercase? Kind regards, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I guess a way forward is to add the Pre Function (https://docs.konghq.com/hub/kong-inc/serverless-functions/) plugin in this case.. I'll test with it. |
Beta Was this translation helpful? Give feedback.
-
@tobiasehlert if you want to simply normalize/rewrite the uri in-place, this is possible by using the request-transformer plugin with its advanced template feature: https://docs.konghq.com/hub/kong-inc/request-transformer/#advanced-templates The uri/path is not injected into the template environment by default, so we'll need to use a route with a uri capture to make it available: ---
name: my-route
paths:
# this makes `uri_captures.person` accessible to our request-transformer template
- "/api/greeting/(?<person>.+)"
service: ...
plugins:
- name: request-transformer
config:
replace:
# here we use the Lua `string.lower()` metamethod to normalize our API path to lowercase:
uri: "/api/greeting/$(uri_captures.person:lower())" The other strategy might be to return a 301 redirect to the proper, normalized uri, but that would require using the pre-function plugin with a little more effort. |
Beta Was this translation helpful? Give feedback.
@tobiasehlert if you want to simply normalize/rewrite the uri in-place, this is possible by using the request-transformer plugin with its advanced template feature:
https://docs.konghq.com/hub/kong-inc/request-transformer/#advanced-templates
The uri/path is not injected into the template environment by default, so we'll need to use a route with a uri capture to make it available: