Rewrite and redirect virtualservice

#1

Is it possible to do a rewrite and then redirect.

My goal is to keep the same path in users browser.

Frontend = example.com/app1
Backend service app1 is serving on “/”

I can rewrite /app1 to /
But the static images all failing as they cant find the pod service at example.com/images but if I typing in example,com/app1/images it works.

So thinking a rewrite and redirect might work.
Is this possible in istio, it works with nginx

0 Likes

#2

istio support url rewrite

0 Likes

#3

But does it support a rewrite and redirect

0 Likes

#4

Haven’t use this kind of feature, but from VirtualService API docs, it does support rewrite and redirect

0 Likes

#5

Rewrite and redirect together in the same match give a validation error:

admission webhook "pilot.validation.istio.io" denied the request:
configuration is invalid: HTTP route cannot contain both route and redirect

I was able to get it to work by doing them in different match blocks. This works but is cumbersome. Here is an example that works with Bookinfo. If the user does not specify a valid Bookinfo PATH this rule routes to httpbin and redirects to a particular URL on httpbin.

(You need to install bookinfo, bookinfo-gateway, httpbin, but not httpbin-gateway for my example rule to do anything interesting)

cat <<EOF | kubectl apply -f -
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*"
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    route:
    - destination:
        host: productpage
        port:
          number: 9080
  - match:
    - uri:
        prefix: /base64/
    route:
    - destination:
        host: httpbin
        port:
          number: 8000
  - match:
    - uri:
        prefix: /
    redirect:
      uri: /base64/Qm9va2luZm8gaXMgPGEgaHJlZj0nL3Byb2R1Y3RwYWdlJz5oZXJlPC9hPgo=
EOF
0 Likes