NGINX 控制器 Kubernetes:需要更改入口内的 Host 标头 [英] NGINX controller Kubernetes: need to change Host header within ingress

查看:22
本文介绍了NGINX 控制器 Kubernetes:需要更改入口内的 Host 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 NGINX 和 Kubernetes.我需要配置 NGINX,以便它设置或覆盖 Host 标头 Host: minio:9000 到 Pod,以便 Pod 将始终服务请求,认为它作为 minio:9000 无论请求来自何处.我相信推荐的方法是使用 NGINX 并修改该 Pod 的入口注释(也许我错了).

I'm running NGINX and Kubernetes. I need to configure NGINX so that it sets or overrides the Host header Host: minio:9000 to a pod so that the Pod will always service requests thinking that it's hosting as minio:9000 no matter where the request is coming from. I believe the recommended approach is to use NGINX and modify the ingress annotations of that Pod (maybe I'm wrong).

我通过本指南代替我的 nginx 控制器与掌舵 https://kubernetes.github.io/ingress-nginx/deploy/#using-helm

I instead my nginx controller with helm via this guide https://kubernetes.github.io/ingress-nginx/deploy/#using-helm

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm install my-release ingress-nginx/ingress-nginx

我这样声明了我的入口.指南建议我在入口 YAML 上使用 nginx 注释.

I declared my ingress like this. Guides have suggested I use nginx annotations on the ingress YAML.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: {{ template "fullname" . }}
  labels:
    chart: {{ template "chartVersion" . }}
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      more_set_input_headers 'Host: minio:9000';

spec:
  rules:
  - host: {{ .Values.ingress.host }}
    http:
      paths:
      - path: /
        backend:
          serviceName: {{ template "fullname" . }}
          servicePort: {{ .Values.deployment.servicePort }}

许多指南都说这行得通.我试过了

Many guides have said this would work. I've tried

nginx.ingress.kubernetes.io/upstream-vhost: minio:9000

我也试过

nginx.ingress.kubernetes.io/configuration-snippet: |
  access_by_lua_block {
    ngx.var.best_http_host = 'minio:9000';
  }

这些方法似乎都不会从集群内部更改主机标头.如果我这样做

None of these methods seem to change the host header from inside the cluster. If I do

nginx.ingress.kubernetes.io/configuration-snippet: |
  proxy_set_header Host 'minio:9000';

我收到 400 响应,说 Host 标头太多(这似乎是由运行 Go 的 Pod 引起的).但至少我知道 nginx 正在接收这些注释.

I get a 400 response saying too many Host headers (which seems to be caused by the Pod running Go). But at least I know the annotations are being picked up by nginx.

  • 如何在 kubernetes 中配置 NGINX,以便它从入口内部更改 Host 标头?
  • 也许我以错误的方式安装了 NGINX?如果通过 helm 安装,从 NGINX 修改 conf 文件似乎并不容易.手动安装 NGINX 而不是通过 k8s 或 helm 安装会更好吗?

推荐答案

原来是我的另一个注解 nginx.ingress.kubernetes.io/rewrite-target:/$1 剥离了里面的路径集群.去掉这个注解会使入口按预期工作.

Turns out it was by my other annotation nginx.ingress.kubernetes.io/rewrite-target: /$1 stripping the path inside the cluster. Getting rid of this annotation makes the ingress work as intended.

这篇关于NGINX 控制器 Kubernetes:需要更改入口内的 Host 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆