regex in URI for proxy passing

泪湿孤枕 提交于 2019-12-11 04:48:48

问题


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).*$'
    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.


回答1:


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.



来源:https://stackoverflow.com/questions/56581304/regex-in-uri-for-proxy-passing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!