Is it possible to rewrite HOST header in k8s Ingress Controller?

16,169

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
16,169
Agung Pratama
Author by

Agung Pratama

I am Bachelor's degree in Computer Science from Faculty of Computer Science, Universitas Indonesia. Currently working as a Software Engineer and also DevOps in Data Team at Traveloka.

Updated on June 07, 2022

Comments

  • Agung Pratama
    Agung Pratama about 2 years

    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
    Agung Pratama over 5 years
    Hi @Harsh, sorry but i don't think Nginx Ingress Controller support overwrite the Host header. Can you show the example? AFAIK, it only supports uri path rewrite
  • Harsh Manvar
    Harsh Manvar over 5 years
    yes it's only support uri path ...you want to write ?
  • Harsh Manvar
    Harsh Manvar over 5 years
    i think with this annotation you can do it. nginx.ingress.kubernetes.io/rewrite-target: /
  • Agung Pratama
    Agung Pratama over 5 years
    Hi Harsh, I want to rewrite the Host/Authority header. in vanilla nginx, it can be done by proxy_set_header Host <custom-host>;.
  • Harsh Manvar
    Harsh Manvar over 5 years
    thankyou so much for sharing i get to know something not used vanilla nginx ..thanks
  • Agung Pratama
    Agung Pratama over 5 years
    Thanks for that configuration-snippet suggestion. Although it still isn't answering my question, your answer is still useful for other similar use cases.
  • Drew
    Drew over 4 years
    I get errors about multiple host headers being set when using this. The nginx.conf contains double proxy_set_header Host with this snippet
  • Shabab Qaisar
    Shabab Qaisar over 3 years
    This doesn't work. Keycloak throws 400 Bad request after adding this annotation to ingress.