4

I have an Istio 1.4.6 VirtualService with a match and a url rewrite defined as follows:

    match:
    - authority:
        prefix: example.com
      uri:
        prefix: /foo/bar
    rewrite:
      uri: /
    route:
    - destination:
        host: some-service
        port:
          number: 80

I would like a rewrite like follows:

Traffic directed to /foo/bar or any subpath of that should be rewritten to / plus any subpath on some-service.

i.e.
example.com/foo/bar -> some-service
example.com/foo/bar/subpath -> some-service/subpath
example.com/foo/bar/subpath/anothersubpath -> some-service/subpath/anothersubpath

However, when I sysdig the traffic coming into some-service, I see that Istio has rewritten the path to:

GET //subpath/anothersubpath HTTP/1.1

Notice the two slashes in the GET request. In the VirtualService spec rewrite.uri field, I can't seem to leave that field empty or add an empty string there. Doing so causes the resource not to validate.

I.e. I can NOT do this:

    rewrite:
      uri: ""

And can NOT do this

    rewrite:
      uri:

How can I define a VirtualService rewrite to send traffic to the root of the destination service? Any help is much appreciated.

Joe J
  • 9,985
  • 16
  • 68
  • 100

2 Answers2

10

There is a github issue about this.

The simplest fix is to add whitespace in the uri as long as You are not running .net core application.

    rewrite:
      uri: " "

Other workaround can be found here,

Hope it helps.

Piotr Malec
  • 3,429
  • 11
  • 16
  • Thank you very much for that answer. Good to know there are several workarounds and an issue for it. – Joe J Mar 12 '20 at 20:18
8

another way of doing this is:

    - match:
        - uri:
            prefix: "/v2/my-service/"
        - uri:
            exact: "/v2/my-service"
      rewrite:
        uri: "/"
      route:
        - destination:
            host: my-internal-service
            port:
              number: 6666
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • Thank you. The only method of listed here which works in my Istio 1.9 :/ – Maciek Leks Apr 30 '21 at 06:50
  • I want to bump this answer. It was the only thing that worked for me. I have a service that will receive calls at `myservice` and `myservice/` and `myservice/some-path` and this solution handles all of them correctly. – Alex Jun 15 '21 at 19:08