建立相对于新路径的响应链接

RAb*_*ham 4 nginx kubernetes nginx-ingress

我如何重定向我的所有hrefs回复以走上新的道路。例如,我的入口文件是

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-odin
  annotations:    
    nginx.ingress.kubernetes.io/rewrite-target: /$1

spec:
  rules:
  - http:
      paths:
      - path: /odin/?(.*)
        backend:
          serviceName: flask-app-tutorial
          servicePort: 8080
Run Code Online (Sandbox Code Playgroud)

当我访问https://mysite/odin页面时。它起作用并返回响应:

HTML 响应是:

<html>
..
    <body>
        <div>
            <a href="/v0/index">Home</a>
            <a href="/v0/login">Login</a>
        </div>

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

但是,正如您所看到的,相对链接就像<a href="/v0/index">Home</a>. 如果我点击它,它将不起作用,因为没有像 之类的链接http://mysite/v0/index。如果我点击链接,我希望它转到http://mysite/odin/v0/index. 是否可以通过修改响应中的链接来获得,odin或者如果我单击它,它会查看源网址,即http://mysite/odin并将其相对于该网址进行定向?

Nginx Version: 1.15.10
ingress-nginx: 0.24.0
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经尝试了以下方法。

nginx.ingress.kubernetes.io/configuration-snippet: |
  proxy_set_header Accept-Encoding ""; #disable compression
  sub_filter '<head>' '<head> <base href="/odin/">';


nginx.ingress.kubernetes.io/add-base-url: ":true"
nginx.ingress.kubernetes.io/app-root: /odin

nginx.ingress.kubernetes.io/use-regex: "true"
Run Code Online (Sandbox Code Playgroud)

我也尝试过这个

 change the spec.rules.host.paths.path from /odin/?(.*) to/(odin/.*)
Run Code Online (Sandbox Code Playgroud)

上面的建议可能有错别字。我认为应该http代替host.

小智 7

我遇到了类似的问题,并受到您的问题的启发,我找到了一个对我有用的解决方案。你的注释应该像这样工作:

nginx.ingress.kubernetes.io/configuration-snippet: |
  sub_filter_once on;
  sub_filter '<base href="/"' '<base href="/odin/"';
Run Code Online (Sandbox Code Playgroud)

如您所见, sub_filter 现已启用,我使用base而不是head