NGINX controller Kubernetes: need to change Host header within ingress

ghz 1years ago ⋅ 928 views

Question

Scenario

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).

How I currently set it up

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

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

I've also tried

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';

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.

Questions

  • How can I configure NGINX in kubernetes so that it changes the Host header from inside the ingress?
  • Perhaps I installed NGINX the wrong way? It doesn't seem easy to modify the conf files from NGINX if you install it through helm. Is it better to install NGINX manually and not through k8s or helm?

Answer

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.