为什么 URL 当我最后不使用斜杠时重写不起作用?
Why URL re-writing is not working when I do not use slash at the end?
我有一个简单的入口配置文件-
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /link2/link3/
name: tut-ingress
namespace: default
spec:
rules:
- host: tutorial.com
http:
paths:
- path: /link1/
pathType: Prefix
backend:
service:
name: nginx-ingress-tut-service
port:
number: 8080
其中到达 /link1
或 /link1/
的请求被重写为
/link2/link3/
。
当我使用 http://tutorial.com/link1/
访问它时
我看到了正确的结果,但是当我使用它访问它时
http://tutorial.com/link1
,我收到 404 未找到。
nginx-ingress-tut-service
具有以下端点-
/
/link1
/link2/link3
我是 web 领域的初学者,任何帮助将不胜感激。
当我把它改成-
- path: /link1
它开始工作正常,但谁能告诉我为什么它不能与 /link1/
一起工作。
有用的资源 -
https://kubernetes.io/docs/concepts/services-networking/ingress/#examples
https://kubernetes.github.io/ingress-nginx/examples/rewrite/
编辑 - 请同时解释当您在 link 中编写完整的 HTTP 时会发生什么
nginx.ingress.kubernetes.io/rewrite-target
答案发表在评论中:
Well, /link1/
is not a prefix of /link1
because a prefix must be the same length or longer than the target string
如果你有
- path: /link1/
要匹配的字符串必须在路径末尾有一个 /
字符。一切正常。在这种情况下,如果您尝试通过 link http://tutorial.com/link1
访问,您将收到 404 错误,因为入口期望 http://tutorial.com/link1/
.
更多可以看examples of rewrite rule and documentation about path types:
Each path in an Ingress is required to have a corresponding path type. Paths that do not include an explicit pathType
will fail validation. There are three supported path types:
ImplementationSpecific
: With this path type, matching is up to the IngressClass. Implementations can treat this as a separate pathType
or treat it identically to Prefix
or Exact
path types.
Exact
: Matches the URL path exactly and with case sensitivity.
Prefix
: Matches based on a URL path prefix split by /
. Matching is case sensitive and done on a path element by element basis. A path element refers to the list of labels in the path split by the /
separator. A request is a match for path p if every p is an element-wise prefix of p of the request path.
编辑:
根据文档,这应该有效,但看起来有一个 fresh problem with nginx ingress. The problem is still unresolved. You can use workaround posted in this topic 或尝试像这样改变你的你:
- path: /link1(/|$)
我有一个简单的入口配置文件-
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /link2/link3/
name: tut-ingress
namespace: default
spec:
rules:
- host: tutorial.com
http:
paths:
- path: /link1/
pathType: Prefix
backend:
service:
name: nginx-ingress-tut-service
port:
number: 8080
其中到达 /link1
或 /link1/
的请求被重写为
/link2/link3/
。
当我使用 http://tutorial.com/link1/
访问它时
我看到了正确的结果,但是当我使用它访问它时
http://tutorial.com/link1
,我收到 404 未找到。
nginx-ingress-tut-service
具有以下端点-
/
/link1
/link2/link3
我是 web 领域的初学者,任何帮助将不胜感激。
当我把它改成-
- path: /link1
它开始工作正常,但谁能告诉我为什么它不能与 /link1/
一起工作。
有用的资源 - https://kubernetes.io/docs/concepts/services-networking/ingress/#examples
https://kubernetes.github.io/ingress-nginx/examples/rewrite/
编辑 - 请同时解释当您在 link 中编写完整的 HTTP 时会发生什么
nginx.ingress.kubernetes.io/rewrite-target
答案发表在评论中:
Well,
/link1/
is not a prefix of/link1
because a prefix must be the same length or longer than the target string
如果你有
- path: /link1/
要匹配的字符串必须在路径末尾有一个 /
字符。一切正常。在这种情况下,如果您尝试通过 link http://tutorial.com/link1
访问,您将收到 404 错误,因为入口期望 http://tutorial.com/link1/
.
更多可以看examples of rewrite rule and documentation about path types:
Each path in an Ingress is required to have a corresponding path type. Paths that do not include an explicit
pathType
will fail validation. There are three supported path types:
ImplementationSpecific
: With this path type, matching is up to the IngressClass. Implementations can treat this as a separatepathType
or treat it identically toPrefix
orExact
path types.
Exact
: Matches the URL path exactly and with case sensitivity.
Prefix
: Matches based on a URL path prefix split by/
. Matching is case sensitive and done on a path element by element basis. A path element refers to the list of labels in the path split by the/
separator. A request is a match for path p if every p is an element-wise prefix of p of the request path.
编辑: 根据文档,这应该有效,但看起来有一个 fresh problem with nginx ingress. The problem is still unresolved. You can use workaround posted in this topic 或尝试像这样改变你的你:
- path: /link1(/|$)