17

Due to some legacy application that relies on Host header to function correctly, I need to have an Ingress (proxy, etc) that capable of rewrite Host header and pass that to downstream (backend). Is there any Ingress Controller that supports this functionality?

Example:

End user access our website through foo.com/a for backend a and foo.com/b for backend b. But since a and b are legacy app, it only accept:

  • a accepts connection when Host: a.foo.com
  • b accepts connection when Host: b.foo.com
Agung Pratama
  • 3,666
  • 7
  • 36
  • 77

4 Answers4

34

This can be done using this annotation: nginx.ingress.kubernetes.io/upstream-vhost: host.example.com

Camil
  • 7,800
  • 2
  • 25
  • 28
4

I'm not sure whether you can find appropriate annotation within NGINX Ingress Controller for Host header modification to match your requirement as well. However, you can consider using nginx.ingress.kubernetes.io/configuration-snippet annotation in order to append configuration snippet to the location block inside nginx.conf of the particular Nginx controller pod:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header Host www.example-host.com;
  name: my-app
spec:
  rules:
  - host: my-app.example.com
    http:
      paths:
      - backend:
        path: /app
          serviceName: my-app
          servicePort: http

We set here Host header www.example-host.com for target URL my-app.example.com.

Nick_Kh
  • 5,089
  • 2
  • 10
  • 16
  • Thanks for that `configuration-snippet` suggestion. Although it still isn't answering my question, your answer is still useful for other similar use cases. – Agung Pratama Feb 12 '19 at 12:06
  • 1
    I get errors about multiple host headers being set when using this. The nginx.conf contains double proxy_set_header Host with this snippet – Drew Dec 02 '19 at 20:00
  • This doesn't work. Keycloak throws 400 Bad request after adding this annotation to ingress. – Shabab Qaisar Mar 17 '21 at 12:27
  • Do not use this, use the upstream-vhost annotation above. I lost a lot of time to find out ingress-nginx creates 2 Host headers in this case, which gave an unclear 400 in the upstream server – Rémi Debette Apr 18 '23 at 07:13
1

I want to add my finding to this question of mine.

Although my solution is not using k8s Ingress Controller, our cluster is using Istio and Istio's VirtualService supports rewrite the uri and authority (Host header) as documented in this link: https://istio.io/docs/reference/config/istio.networking.v1alpha3/#HTTPRewrite

To know how I implement that in my case, you can take a look at this link: https://github.com/istio/istio/issues/11668

Agung Pratama
  • 3,666
  • 7
  • 36
  • 77
0

you can use ingress nginx controller on Kubernetes and set head and also transfer to backend and manage services connection from ingress objects.

here sharing link for rewrite target from header: https://kubernetes.github.io/ingress-nginx/examples/rewrite/

ingress nginx will be also good with SSL cert manager you can add it.

manage other thing using annotations of ingress.

check this out for ingress SSL setup you can modify it and per your need: https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nginx-ingress-with-cert-manager-on-digitalocean-kubernetes

ingress will be like at last

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - hosts:
    - myapp.abc.com
    secretName: ingress-tls
  rules:
  - host: myapp.abc.com
    http:
      paths:
      - path: /my-service
        backend:
          serviceName: my-backend
          servicePort: 80
Freshleaf Media
  • 382
  • 4
  • 10
Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102