How do I redirect all my hrefs
within my response to hit my new path. For e.g., my ingress file is
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
When I visit the page at https://mysite/odin. It works and returns back the response:
The HTML response is:
<html>
..
<body>
<div>
<a href="/v0/index">Home</a>
<a href="/v0/login">Login</a>
</div>
</body>
</html>
However, as you can see, the relative links are like <a href="/v0/index">Home</a>
. If I click on that, it won't work since there is no link like http://mysite/v0/index
. If I click on the link, I want it to go to http://mysite/odin/v0/index
. Is it possible either by modifying the links in the response to have odin
or if I do click on it, it looks at the source url i.e. http://mysite/odin
and direct it relative to that?
Nginx Version: 1.15.10
ingress-nginx: 0.24.0
So far,I have tried the following.
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"
I have also tried this i.e.
change the spec.rules.host.paths.path from /odin/?(.*) to/(odin/.*)
There may be a typo in the advice above. I think it should http
instead of host
.
答案 0 :(得分:2)
我遇到了类似的问题,并且受您的问题启发,我找到了一个对我有用的解决方案。 您的注释应如下所示:
nginx.ingress.kubernetes.io/configuration-snippet: |
sub_filter_once on;
sub_filter '<base href="/"' '<base href="/odin/"';
如您所见,sub_filter现在已启用,我使用了 base 而不是 head 。
答案 1 :(得分:1)
即使您按照其他答案中的建议通过包含sub_filter
来修复sub_filter_once on;
配置代码片段,也不会起作用,因为base
标签仅适用于相对路径(请参阅: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base。
app-root
解决方案在技术上是正确的,但归结为与<base href=... />
相同,因此对于href
标签带有反斜杠也将失败。
我可以看到两种可能性:
1)您可以修复所有href
标签,删除前导斜杠。如果这样做,两种解决方案都应该起作用。
2)您可以尝试将位置规则更改为- path: /(odin)?/?(.*)
,并将重写注解更改为:nginx.ingress.kubernetes.io/rewrite-target: /$2
(不要忘记也包含nginx.ingress.kubernetes.io/use-regex: "true"
注解),它将与也是一个斜杠。
它看起来很脆弱,因为该规则现在将(几乎)匹配所有内容,并且您可以放弃使用具有不同路径前缀的NGINX散开的可能性。
对“ 2)”进行修改以使其更可行的方法是,如果/v0
标签之间的前缀一致,则匹配href
前缀,而不是基本匹配。