regex in URI for proxy passing

6/13/2019

I have following config in my nginx:

location / {
    if ($request_uri ~* ^/checkout/(dev-dist|dist|images|js|libs|resources|angular4-hybrid|bundle.js)) {

proxy_pass http://static-qa-uscentral1.company.com/hybrid/live$request_uri;
            break;
        }
}

I am trying to replicate this in istio's virtual service

I have written following virtual service to match this regex:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: routes-static
  namespace: mui-relqa
spec:
  gateways:
  - my-gateway
  hosts:
  - "*"
  http:
  - match:
    - uri:
        regex: '^./checkout/(dev-dist|dist|images|js|libs|resources|angular4-hybrid|bundle.js).*
#x27;
redirect: authority: static-qa-uscentral1.company.com uri: /hybrid/live

Few things I would lobe to have clarity on:

  1. how to use that $request_uri used in nginx config to replicate in virtual service

  2. using the above virtual service it will totally redirect the calls to "static-qa-uscentral1.company.com" which I have mentioned in "authority" parameter in "virtualservice" yaml. How can I achieve what nginx does during proxy_pass which doesnot change the URL but still gets the content of redirected URL.

-- Stunn3r
istio
kubernetes

1 Answer

6/19/2019

You could probably use Istio Envoy filter, you might want to check other rewrite options for Envoy HTTP routing.

You can have a look at Katacoda Migrating from NGINX to Envoy Proxy. In step 4 they show example of proxy_pass.

Regex will match ECMAscript style regex-based, you can even have a look into Istio Virtual Service source code.

You would need to either remove Istio and setup NGINX Ingress Controller instead, or setup the Ingress Controller behind Istio so it would redirect and/or proxy the traffic based on nginx.conf or using Nginx Annotations.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
 set $agentflag 0;

 if ($http_user_agent ~* "(Mobile)" ){
 set $agentflag 1;
 }

 if ( $agentflag = 1 ) {
 return 301 https://m.example.com;
 }

Update

This was mentioned by the OP, Envoy also supports Lua scripting that allows essentially injecting arbitrary code in the proxy to process requests.

-- Crou
Source: StackOverflow