我有一个名为 - backend-service 的后端服务。 该服务的 Pod 执行基于主机的路由,就像从 Pod 中运行时一样 -
curl --location 'http://localhost:9900/api/foo' \
--header 'Host: xyz.example.com' \
--header 'Content-Type: application/json' \
--data-raw '{
"foo": "bar"
}'
这提供了 200 状态。
现在我有一个主机
bar.example.com
的入口规则,它配置了此后端服务。
当我使用标头“Host: foo.example.com”访问 bar.example.com
时,nginx 给出 404 错误。
使用的命令 -
curl --location 'http://bar.example.com/api/foo' \
--header 'Host: xyz.foo.com' \
--header 'Content-Type: application/json' \
--data-raw '{
"foo": "bar"
}'
以下是我的入口注释 -
nginx.ingress.kubernetes.io/affinity: cookie
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
nginx.ingress.kubernetes.io/session-cookie-name: SESSION
ingress-nginx-controller 配置图 -
allow-snippet-annotations: "true"
enable-access-log-for-default-backend: "true"
enable-underscores-in-headers: "true"
large-client-header-buffers: 4 100k
proxy-real-ip-cidr: 10.1.2.3/23,10.1.2.4/24
use-forwarded-headers: "true"
nginx 似乎没有保留从客户端传递的主机头。 这里可能有什么问题?
我设法通过为该虚拟主机添加另一个具有通配符主机支持的入口规则来解决此问题 - https://kubernetes.io/docs/concepts/services-networking/ingress/#hostname-wildcards
- host: "*.foo.com"
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: {{ .Values.service }}
port:
number: {{ .Values.ports.http }}