在 Istio 网关上使用 url 重写时资源的 HTTP404

sky*_*ner 2 url-rewriting istio

我正在尝试将服务部署到 AKS 上的特定 url 地址。以下 yaml 让我在所需地址访问服务,例如 xxxx.europe.cloudapp.azure.com/service-a。这很好用,我设法将整个服务隐藏在所需的 url 下:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: istio-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: istio
spec:
  hosts:
  - "*"
  gateways:
  - istio-gateway
  http:
  - match:
    - uri:
        prefix: /service-a
    rewrite:
      uri: /    
    route:
    - destination:
        host: service-a.default.svc.cluster.local
Run Code Online (Sandbox Code Playgroud)

但是,当显示欢迎页面时,我只能看到文字。没有加载 css/javascript/image 文件。此页面尝试加载的所有内容仍然具有原始 url 地址,而我的网关配置没有进行任何重写。所以主页要求:

http://xxxxx.europe.cloudapp.azure.com/icon.jpg
Run Code Online (Sandbox Code Playgroud)

而不是这个:

http://xxxxx.europe.cloudapp.azure.com/service-a/icon.jpg
Run Code Online (Sandbox Code Playgroud)

处理页面上资源和链接的重写 url 的最佳方法是什么?我必须手动更改主页上的网址吗?

编辑:

要更清楚。

  1. url 的重写按预期工作,地址正是我想要的(整个服务隐藏在“xxxx.europe.cloudapp.azure.com/service-a”下。
  2. 一旦我进入“xxxx.europe.cloudapp.azure.com/service-a”,我就会看到服务的欢迎页面,但没有加载任何 css/jpegs/js。此外,欢迎页面上可见的链接也不起作用。
  3. 例如,未加载“icon.jpg”。页面想从http://xxxx.europe.cloudapp.azure.com/icon.jpg加载它,但它不再存在。由于重写,它可以在http://xxxx.europe.cloudapp.azure.com/service-a/icon.jpg上获得,正如预期的那样。

我有点期望http://xxxx.europe.cloudapp.azure.com/icon.jpg请求会自动重写为http://xxxx.europe.cloudapp.azure.com/service-a/icon.jpg . 但显然我错了。所以我想知道如何以可管理的方式处理服务本身中的链接 - 我的意思是我可以修改应用程序中的每个可能的链接,但是如果我们再次更改 url(从 /service-a 到 /service-b)怎么办. 该服务是用 ASP.NET Core 编写的,我将寻找某种可维护的内部重写解决方案。

Rin*_*nor 7

由于这部分配置,正在发生重写:

  - match:
    - uri:
        prefix: /service-a
    rewrite:
      uri: /  
Run Code Online (Sandbox Code Playgroud)

这导致匹配的前缀被替换为rewrite.uri属性的值。

例1:(虚拟服务被激活)

Original: http://www.page.com/service-a/icon.jpg
                             ^--------^
Rewritten: http://www.page.com/icon.jpg
Run Code Online (Sandbox Code Playgroud)

例2:(这个虚拟服务被激活)

Original: http://www.page.com/service-a/service-a/icon.jpg
                             ^--------^
Rewritten: http://www.page.com/service-a/icon.jpg
Run Code Online (Sandbox Code Playgroud)

例3:(这个虚拟服务没有被激活,回退到其他虚拟服务,或者在默认路由上,或者返回404的黑洞)

Original: http://www.page.com/icon.jpg

Rewriting: DOESN'T HAPPEN
Run Code Online (Sandbox Code Playgroud)

对于重写,没有建议也不能,这取决于您的服务。可以在这里找到 Istio 重写 props 的文档

如果每个子域都有自己的服务,那么这将是一个选项:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: istio
spec:
  hosts:
  - "service-a.domain.com"
  gateways:
  - istio-gateway
  http:
  - match:
    - uri:
        prefix: /
    rewrite:
      uri: /service-a
    route:
    - destination:
        host: service-a.default.svc.cluster.local
Run Code Online (Sandbox Code Playgroud)