Upstream URL Rewrite

Hi,

I have a use-case where I need to rewrite an Upstream URL to route to a specific backend service in a cluster. We will receive a dynamic variable in the POST request to our TYK gateway something like

https://gateway/models/{var}

The dynamic upstream URL I need to then create will follow something like http://{var}.svc.cluster/ extracting the {var} value from the request URL into the TYK gate

I’ve had a look at the URLRewrite plugin however this sets the upstream url to a constant value and then appends the rewrite logic at the end of that url instead of changing the upstream url completely. Any ideas to achieve this?

Thanks

Hello and welcome to the community :partying_face:

It appears you are already on the right track with the rewrite and regex logic. For the dynamism have you checked that our doc doesn’t have what you are looking for?

Hi Olu,

Thanks for replying! I’ve solved the original issue I was facing thanks to your help :smiley:

Only small issue now I’m facing is that I cant figure out how to pass wildcards into the URL rewrite

I have as a rewrite rule to extract {model-name}
Match Pattern
model-service/v1/completions/{provider}/{model-name}

I’ve tried 3 rewrite patterns:
Rewrite to
http://example/$tyk_context.path_parts-4
http://example/{model-name}
http://example/$2

None of these seem to work. Do you have any idea how to take the wildcard {model-name} and pass it into the rewrite?

Thanks
none of these seem to wrok

I believe you have to indicate the wild card in the pattern rather than use the key name since it would be ignored as specified in the docs

The wildcard value itself will be ignored, is not extracted from the key, and is not 
available for use in constructing the rewrite path.

You should be able to still get what you want by replacing the {value} with (.*)

Match pattern: /completions/(.*)/(.*)
Rewrite to: <relative or absolute URL>$2/$1

Request: /completions/123/ABC
Result: /completions/ABC/123