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

查看:127
本文介绍了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标头(这似乎是Pod运行Go引起的).但是至少我知道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?如果通过头盔进行安装,从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天全站免登陆