How to rewrite url in istio virtual service

422 Views Asked by At

How can we rewrite the url in virtual service. eg:

Actual url----> http//test.com/service1/v1/list
rewrite url path---->/v1/list

After matching the path based routing with prefix "service1", I want to remove the second value of the url, in this case "service1" and send rest of the path(/v1/list) to the destination service.

i have tried the below VS, but it didnt worked,

spec:
  hosts:
  - test.com
  http:
  - match:
    - uri:
        prefix: /service1
    rewrite:
      uriRegexRewrite:
        match: /service1(/|$)(.*)
        rewrite: $2    
    route:
    - destination:
        host: service1.default.svc.cluster.local

Are there any mistakes in the "match" and "rewrite" methods for eliminating the second value from the URL while keeping the other values?

1

There are 1 best solutions below

1
On

Based on the information provided, you may try this approach:

apiVersion: [networking.istio.io/v1alpha3]
kind: VirtualService 
metadata: 
    name: service1-virtual-service 
spec: 
  hosts:
  - test.com 
  http:
  -match:
   - uri: 
      prefix: "/service1" 
   rewrite: 
     uri: "/v1/list" 
   route:
   - destination: 
       host: service1.default.svc.cluster.local

This example uses the prefix to match requests starting with /service1 and then rewrite the URI to /v1/list which is a fit for your use case.